Part Number Hot Search : 
TLN233 PE4274 NJU7332 HYS72T 725UY3 SI4925DY ML2721DH C1330HA
Product Description
Full Text Search
 

To Download PIC18F2431 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  ? 2003 microchip technology inc. preliminary ds39616b pic18f2331/2431/4331/4431 data sheet 28/40/44-pin enhanced flash microcontrollers with nanowatt technology, high performance pwm and a/d
ds39616b-page ii preliminary ? 2003 microchip technology inc. information contained in this publication regarding device applications and the like is intended through suggestion only and may be superseded by updates. it is your responsibility to ensure that your application m eets with your specifications. no representation or warranty is given and no liability is assumed by microchip technol ogy incorporated with respect to the accuracy or use of such information, or infringement of patents or other intellectual property rights arising from such use or otherwise. use of mi crochip?s products as critical components in life support syst ems is not authorized except with express written approval by microchip. no licenses are conveyed, implicitly or ot herwise, under any intellectual property rights. trademarks the microchip name and logo, the microchip logo, accuron, dspic, k ee l oq , mplab, pic, picmicro, picstart, pro mate and powersmart are registered trademarks of microchip technology incorporated in the u.s.a. and other countries. amplab, filterlab, micro id , mxdev, mxlab, picmaster, seeval, smartshunt and the embedded control solutions company are registered trademarks of microchip technology incorporated in the u.s.a. application maestro, dspicdem, dspicdem.net, dspicworks, ecan, economonitor, fansense, flexrom, fuzzylab, in-circuit serial programming, icsp, icepic, microport, migratable memory, mpasm, mplib, mplink, mpsim, pickit, picdem, picdem.net, pictail, powercal, powerinfo, powermate, powertool, rflab, rfpic, select mode, smartsensor, smarttel and total endurance are trademarks of microchip technology incorporated in the u.s.a. and other countries. serialized quick turn programming (sqtp) is a service mark of microchip technology incorporated in the u.s.a. all other trademarks mentioned herein are property of their respective companies. ? 2003, microchip technology incorporated, printed in the u.s.a., all rights reserved. printed on recycled paper. note the following details of the code protection feature on microchip devices:  microchip products meet the specification cont ained in their particular microchip data sheet.  microchip believes that its family of products is one of the mo st secure families of its kind on the market today, when used i n the intended manner and under normal conditions.  there are dishonest and possibly illegal methods used to breach the code protection feature. all of these methods, to our knowledge, require using the microchip produc ts in a manner outside the operating specif ications contained in microchip's data sheets. most likely, the person doing so is engaged in theft of intellectual property.  microchip is willing to work with the customer who is concerned about the integrity of their code.  neither microchip nor any other semicondu ctor manufacturer can guarantee the security of their code. code protection does not mean that we are guaranteeing the product as ?unbreakable.? code protection is constantly evolving. we at microchip are comm itted to continuously improving t he code protection features of our products. attempts to break microchip?s c ode protection feature may be a violation of the digital millennium copyright act. if such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that act. microchip received iso/ts-16949:2002 quality system certification for its worldwide headquarters, design and wafer fabrication facilities in chandler and tempe, arizona and mountain view, california in october 2003 . the company?s quality system processes and procedures are for its picmicro ? 8-bit mcus, k ee l oq ? code hopping devices, serial eeproms, microperipherals, non-volatile memory and analog products. in addition, microchip?s quality system for the design and manufacture of development systems is iso 9001:2000 certified.
? 2003 microchip technology inc. preliminary ds39616b-page 1 14-bit power control pwm module:  up to 4 channels with complementary outputs  edge- or center-aligned operation  flexible dead-band generator  hardware fault protection inputs  simultaneous update of duty cycle and period: - flexible special event trigger output motion feedback module:  three independent input capture channels: - flexible operating modes for period and pulse width measurement - special hall sensor interface module - special event trigger output to other modules  quadrature encoder interface: - 2 phase inputs and one index input from encoder - high and low position tracking with direction status and change of direction interrupt - velocity measurement high-speed, 200 ksps 10-bit a/d converter:  up to 9 channels  simultaneous two-channel sampling  sequential sampling: 1, 2 or 4 selected channels  auto-conversion capability  4-word fifo with selectable interrupt frequency  selectable external conversion triggers  programmable acquisition time flexible oscillator structure:  four crystal modes up to 40 mhz  two external clock modes up to 40 mhz  internal oscillator block: - 8 user selectable frequencies: 31 khz to 8 mhz - osctune can compensate for frequency drift  secondary oscillator using timer1 @ 32 khz  fail-safe clock monitor: - allows for safe shutdown of device if clock fails power-managed modes:  run cpu on, peripherals on  idle cpu off, peripherals on  sleep cpu off, peripherals off  idle mode currents down to 5.8 a typical  sleep current down to 0.1 a typical  timer1 oscillator, 1.8 a typical, 32 khz, 2v  watchdog timer (wdt), 2.1 a typical  two-speed oscillator start-up peripheral highlights:  high current sink/source 25 ma/25 ma  three external interrupts  two capture/compare/pwm (ccp) modules: - capture is 16-bit, max. resolution 6.25 ns (t cy /16) - compare is 16-bit, max. resolution 100 ns (t cy ) - pwm output: pwm resolution is 1 to 10 bits  enhanced usart module: - supports rs-485, rs-232 and lin 1.2 - auto-wake-up on start bit - auto-baud detect  rs-232 operation using internal oscillator block (no external crystal required) special microcontroller features:  100,000 erase/write cycle enhanced flash program memory typical  1,000,000 erase/write cycle data eeprom memory typical  flash/data eeprom retention: 100 years  self-programmable under software control  priority levels for interrupts  8 x 8 single-cycle hardware multiplier  extended watchdog timer (wdt): - programmable period from 41 ms to 131s  single-supply in-circuit serial programming? (icsp?) via two pins  in-circuit debug (icd) via two pins - drives pwm outputs safely when debugging device program memory data memory i/o 10-bit a/d (ch) ccp ssp eusart quadrature encoder 14-bit pwm (ch) timers 8/16-bit flash (bytes) # single-word instructions sram (bytes) eeprom (bytes) spi slave i 2 c? pic18f2331 8192 4096 768 256 24 5 2 y y y y 6 1/3 PIC18F2431 16384 8192 768 256 24 5 2 y y y y 6 1/3 pic18f4331 8192 4096 768 256 36 9 2 y y y y 8 1/3 pic18f4431 16384 8192 768 256 36 9 2 y y y y 8 1/3 28/40/44-pin enhanced flas h microcontrollers with nanowatt technology, high performance pwm and a/d pic18f2331/2431/4331/4431
pic18f2331/2431/4331/4431 ds39616b-page 2 preliminary ? 2003 microchip technology inc. pin diagrams 40-pin pdip 28-pin sdip, soic mclr /v pp /re3 ra0/an0 ra1/an1 ra2/an2/v ref -/cap1/indx ra3/an3/v ref +/cap2/qea ra4/an4/cap3/qeb av dd av ss osc1/clki/ra7 osc2/clko/ra6 rc0/t1oso/t1cki rc1/t1osi/ccp2/flta rc2/ccp1/fltb rc3/t0cki/t5cki/int0 rb7/kbi3/pgd rb6/kbi2/pgc rb5/kbi1/pwm4/pgm (1) rb4/kbi0/pwm5 rb3/pwm3 rb2/pwm2 rb1/pwm1 rb0/pwm0 v dd v ss rc7/rx/dt/sdo rc6/tx/ck/ss rc5/int2/sck/scl rc4/int1/sdi/sda  1 2 3 4 5 6 7 8 9 10 11 12 13 14 28 27 26 25 24 23 22 21 20 19 18 17 16 15 pic18f2331/2431 note 1: low-voltage programming must be enabled. rb7/kbi3/pgd rb6/kbi2/pgc rb5/kbi1/pwm4/pgm (2) rb4/kbi0/pwm5 rb3/pwm3 rb2/pwm2 rb1/pwm1 rb0/pwm0 v dd v ss rd7/pwm7 rd6/pwm6 rd5/pwm4 (4) rd4/flta (3) rc7/rx/dt/sdo (1) rc6/tx/ck/ss rc5/int2/sck (1) /scl (1) rc4/int1/sdi (1) /sda (1) rd3/sck/scl rd2/sdi/sda mclr /v pp /re3 ra0/an0 ra1/an1 ra2/an2/v ref -/cap1/indx ra3/an3/v ref +/cap2/qea ra4/an4/cap3/qeb ra5/an5/lvdin re0/an6 re1/an7 re2/an8 av dd av ss osc1/clki/ra7 osc2/clko/ra6 rc0/t1oso/t1cki rc1/t1osi/ccp2/flta rc2/ccp1/fltb rc3/t0cki (1) /t5cki (1) /int0 rd0/t0cki/t5cki rd1/sdo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 pic18f4331/4431 note 1: rc3 is the alternate pin for t0cki/t5cki; rc4 is the alternate pin for sdi/sda; rc5 is the alternate pin for sck/scl. 2: low-voltage programming must be enabled. 3: rd4 is the alternate pin for flta . 4: rd5 is the alternate pin for pwm4.
? 2003 microchip technology inc. preliminary ds39616b-page 3 pic18f2331/2431/4331/4431 pin diagrams (continued) 44-pin tqfp 10 11 2 3 4 5 6 1 18 19 20 21 22 12 13 14 15 38 8 7 44 43 42 41 40 39 16 17 29 30 31 32 33 23 24 25 26 27 28 36 34 35 9 pic18f4331 37 ra3/an3/v ref +/cap2/qea ra2/an2/v ref -/cap1/indx ra1/an1 ra0/an0 mclr /v pp /re3 nc rb7/kbi3/pgd rb6/kbi2/pgc rb5/kbi1/pwm4/pgm (2) rb4/kbi0/pwm5 nc rc6/tx/ck/ss rc5/int2/sck (1) /scl (1) rc4/int1/sdi (1) /sda (1) rd3/sck/scl rd2/sdi/sda rd1/sdo rd0/t0cki/t5cki rc3/t0cki (1) /t5cki (1) /int0 rc2/ccp1/fltb rc1/t1osi/ccp2/flta nc nc rc0/t1oso/t1cki osc2/clko/ra6 osc1/clki/ra7 av ss av dd re2/an8 re1/an7 re0/an6 ra5/an5/lvdin ra4/an4/cap3/qeb rc7/rx/dt/sdo (1) rd4/flta (3) rd5/pwm4 (4) rd6/pwm6 rd7/pwm7 v ss v dd rb0/pwm0 rb1/pwm1 rb2/pwm2 rb3/pwm3 pic18f4431 note 1: rc3 is the alternate pin for t0cki/t5cki; rc4 is the alternate pin for sdi/sda; rc5 is the alternate pin for sck/scl. 2: low-voltage programming must be enabled. 3: rd4 is the alternate pin for flta . 4: rd5 is the alternate pin for pwm4.
pic18f2331/2431/4331/4431 ds39616b-page 4 preliminary ? 2003 microchip technology inc. pin diagrams (continued) 44-pin qfn 10 11 2 3 4 5 6 1 18 19 20 21 22 12 13 14 15 38 8 7 44 43 42 41 40 39 16 17 29 30 31 32 33 23 24 25 26 27 28 36 34 35 9 pic18f4331 37 ra3/an3/v ref +/cap2/qea ra2/an2/v ref -/cap1/indx ra1/an1 ra0/an0 mclr /v pp /re3 rb3/pwm3 rb7/kbi3/pgd rb6/kbi2/pgc rb5/kbi1/pwm4/pgm (2) rb4/kbi0/pwm5 nc rc6/tx/ck/ss rc5/int2/sck (1) /scl (1) rc4/int1/sdi (1) /sda (1) rd3/sck/scl rd2/sdi/sda rd1/sdo rd0/t0cki/t5cki rc3/t0cki (1) /t5cki (1) /int0 rc2/ccp1/fltb rc1/t1osi/ccp2/flta rc0/t1oso/t1cki osc2/clko/ra6 osc1/clki/ra7 v ss av dd v dd re2/an8 re1/an7 re0/an6 ra5/an5/lvdin ra4/an4/cap3/qeb rc7/rx/dt/sdo (1) rd4/flta (3) rd5/pwm4 (4) rd6/pwm6 rd7/pwm7 v ss v dd av dd rb0/pwm0 rb1/pwm1 rb2/pwm2 pic18f4431 av ss note 1: rc3 is the alternate pin for t0cki/t5cki; rc4 is the alternate pin for sdi/sda; rc5 is the alternate pin for sck/scl. 2: low-voltage programming must be enabled. 3: rd4 is the alternate pin for flta . 4: rd5 is the alternate pin for pwm4.
? 2003 microchip technology inc. preliminary ds39616b-page 5 pic18f2331/2431/4331/4431 table of contents 1.0 device overview ............................................................................................................. ............................................................. 7 2.0 oscillator configurations ..................................... .............................................................. ......................................................... 21 3.0 power-managed modes ......................................................................................................... .................................................... 31 4.0 reset ....................................................................................................................... ................................................................... 45 5.0 memory organization ......................................................................................................... ........................................................ 57 6.0 flash program memory........................................................................................................ ...................................................... 75 7.0 data eeprom memory .......................................................................................................... ................................................... 85 8.0 8 x 8 hardware multiplier ................................................................................................... ........................................................ 89 9.0 interrupts .................................................................................................................. .................................................................. 91 10.0 i/o ports .................................................................................................................. ................................................................. 107 11.0 timer0 module .............................................................................................................. ........................................................... 133 12.0 timer1 module .............................................................................................................. ........................................................... 137 13.0 timer2 module .............................................................................................................. ........................................................... 143 14.0 timer5 module .............................................................................................................. ........................................................... 145 15.0 capture/compare/pwm (ccp) modules .......................................................................................... ....................................... 151 16.0 motion feedback module ..................................................................................................... .................................................... 159 17.0 power control pwm module ................................................................................................... ................................................. 181 18.0 synchronous serial port (ssp) module ....................................................................................... ............................................ 211 19.0 enhanced universal synchronous asynchronous receiver transmitter (eusart) .................................................. ............. 221 20.0 10-bit high-speed analog-to-digital converter (a/d) module................................................................. ................................. 243 21.0 low-voltage detect ......................................................................................................... ......................................................... 261 22.0 special features of the cpu................................................................................................ .................................................... 267 23.0 instruction set summary .................................................................................................... ...................................................... 287 24.0 development support........................................................................................................ ....................................................... 331 25.0 electrical characteristics ................................................................................................. ......................................................... 337 26.0 preliminary dc and ac characteristics graphs and tables.................................................................... ................................ 371 27.0 packaging information...................................................................................................... ........................................................ 373 appendix a: revision history................................................................................................... .......................................................... 379 appendix b: device differences ................................................................................................. ....................................................... 379 appendix c: conversion considerations .......................................................................................... ................................................. 380 appendix d: migration from baseline to enhanced devices........................................................................ ...................................... 380 appendix e: migration from mid-range to enhanced devices ....................................................................... .................................... 381 appendix f: migration from high-end to enhanced devices ........................................................................ ..................................... 381 index .......................................................................................................................... ...................................................................... 383 on-line support................................................................................................................ ................................................................. 391 systems information and upgrade hot line ....................................................................................... ............................................... 391 reader response ................................................................................................................ .............................................................. 392 pic18f2331/2431/4331/4431 product identification system ........................................................................ .................................... 393
pic18f2331/2431/4331/4431 ds39616b-page 6 preliminary ? 2003 microchip technology inc. to our valued customers it is our intention to provide our valued customers with the best documentation possible to ensure successful use of your micro chip products. to this end, we will continue to improve our publications to better suit your needs. our publications will be refined and enhanced as new volumes and updates are introduced. if you have any questions or comments regar ding this publication, please contact the marketing communications department via e-mail at docerrors@mail.microchip.com or fax the reader response form in the back of this data sheet to (480) 792-4150. we welcome your feedback. most current data sheet to obtain the most up-to-date version of this data s heet, please register at our worldwide web site at: http://www.microchip.com you can determine the version of a data sheet by examining its literature number found on the bottom outside corner of any page . the last character of the literature number is the vers ion number, (e.g., ds30000a is version a of document ds30000). errata an errata sheet, describing minor operational differences fr om the data sheet and recommended workarounds, may exist for curren t devices. as device/doc umentation issues become known to us, we will publis h an errata sheet. the errata will specify the revisi on of silicon and revision of document to which it applies. to determine if an errata sheet exists for a particul ar device, please check with one of the following:  microchip?s worldwide web site; http://www.microchip.com  your local microchip sales office (see last page)  the microchip corporate literature center; u.s. fax: (480) 792-7277 when contacting a sales office or the literature center, please specify which device, revision of silicon and data sheet (inclu de literature number) you are using. customer notification system register on our web site at www.microchip.com/cn to receive the most current information on all of our products.
? 2003 microchip technology inc. preliminary ds39616b-page 7 pic18f2331/2431/4331/4431 1.0 device overview this document contains device specific information for the following devices: this family offers the advantages of all pic18 micro- controllers ? namely, high computational performance at an economical price, with the addition of high endur- ance enhanced flash program memory and a high- speed 10-bit a/d converter. on top of these features, the pic18f2331/2431/4331/4431 family introduces design enhancements that make these microcontrol- lers a logical choice for many high performance, power control and motor control applications. these special peripherals include:  14-bit resolution power control pwm module (pcpwm) with programmable dead time insertion  motion feedback module (mfm), including a 3-channel input capture (ic) module and quadrature encoder interface (qei)  high-speed 10-bit a/d converter (hsadc) the pcpwm can generate up to eight complementary pwm outputs with dead-band time insertion. overdrive current is detected by off-chip analog comparators or the digital fault inputs (flta , fltb ). the mfm quadrature encoder interface provides precise rotor position feedback and/or velocity measurement. the mfm 3 x input capture or external interrupts can be used to detect the rotor state for electrically commutated motor applications using hall sensor feedback, such as bldc motor drives. pic18f2331/2431/4331/4431 devices also feature flash program memory and an internal rc oscillator with built-in lp modes. 1.1 new core features 1.1.1 nanow att technology all of the devices in the pic18f2331/2431/4331/4431 family incorporate a range of features that can signifi- cantly reduce power consumption during operation. key items include:  alternate run modes: by clocking the controller from the timer1 source or the internal oscillator block, power consumption during code execution can be reduced by as much as 90%.  multiple idle modes: the controller can also run with its cpu core disabled, but the peripherals are still active. in these states, power consumption can be reduced even further, to as little as 4% of normal operation requirements.  on-the-fly mode switching: the power-man- aged modes are invoked by user code during operation, allowing the user to incorporate power saving ideas into their application?s software design.  lower consumption in key modules: the power requirements for both timer1 and the watchdog timer have been reduced by up to 80%, with typical values of 1.1 and 2.1 a, respectively. 1.1.2 multiple oscillator options and features all of the devices in the pic18f2331/2431/4331/4431 family offer nine different oscillator options, allowing users a wide range of choices in developing application hardware. these include:  four crystal modes, using crystals or ceramic resonators.  two external clock modes, offering the option of using two pins (oscillator input and a divide-by-4 clock output) or one pin (oscillator input, with the second pin reassigned as general i/o).  two external rc oscillator modes, with the same pin options as the external clock modes.  an internal oscillator block, which provides an 8 mhz clock and an intrc source (approxi- mately 31 khz, stable over temperature and v dd ), as well as a range of 6 user-selectable clock fre- quencies (from 125 khz to 4 mhz) for a total of 8 clock frequencies. besides its availability as a clock source, the internal oscillator block provides a stable reference source that gives the family additional features for robust operation:  fail-safe clock monitor: this option constantly monitors the main clock source against a reference signal provided by the internal oscillator. if a clock failure occurs, the controller is switched to the internal oscillator block, allowing for continued low speed operation or a safe application shutdown.  two-speed start-up: this option allows the internal oscillator to serve as the clock source from power-on reset or wake-up from sleep mode, until the primary clock source is available. this allows for code execution during what would otherwise be the clock start-up interval, and can even allow an application to perform routine background activities and return to sleep without returning to full power operation.  pic18f2331  pic18f4331  PIC18F2431  pic18f4431
pic18f2331/2431/4331/4431 ds39616b-page 8 preliminary ? 2003 microchip technology inc. 1.2 other special features  memory endurance: the enhanced flash cells for both program memory and data eeprom are rated to last for many thousands of erase/write cycles ? up to 100,000 for program memory and 1,000,000 for eeprom. data retention without refresh is conservatively estimated to be greater than 100 years.  self-programmability: these devices can write to their own program memory spaces under inter- nal software control. by using a bootloader routine located in the protected boot block at the top of program memory, it becomes possible to create an application that can update itself in the field.  power control pwm module: in pwm mode, this module provides 1, 2 or 4 modulated outputs for controlling half-bridge and full-bridge drivers. other features include auto-shutdown on fault detection and auto-restart to reactivate outputs once the condition has cleared.  enhanced usart: this serial communication module is capable of standard rs-232 operation using the internal oscillator block, removing the need for an external crystal (and its accompany- ing power requirement) in applications that talk to the outside world. this module also includes auto- baud detect and lin capability.  high-speed 10-bit a/d converter: this module incorporates programmable acquisition time, allowing for a channel to be selected and a conversion to be initiated without waiting for a sampling period and thus, reducing code overhead.  motion feedback module (mfm): this module features a quadrature encoder interface (qei) and an input capture (ic) module. the qei accepts two phase inputs (qea, qeb) and one index input (indx) from an incremental encoder. the qei supports high and low precision position tracking, direction status and change of direction interrupt, and velocity measurement. the input capture features 3 channels of independent input capture with timer5 as the time base, a special event trigger to other modules, and an adjustable noise filter on each ic input.  extended watchdog timer (wdt): this enhanced version incorporates a 16-bit prescaler, allowing a time-out range from 4 ms to over 2 minutes, that is stable across operating voltage and temperature.
? 2003 microchip technology inc. preliminary ds39616b-page 9 pic18f2331/2431/4331/4431 1.3 details on individual family members devices in the pic18f2331/2431/4331/4431 family are available in 28-pin (pic18f2x31) and 40/44-pin (pic18f4x31) packages. the block diagram for the two groups is shown in figure 1-1. the devices are differentiated from each other in three ways: 1. flash program memory (8 kbytes for pic18f2x31 devices, 16 kbytes for pic18f4x31). 2. a/d channels (5 for pic18f2x31 devices, 9 for pic18f4x31 devices). 3. i/o ports (3 bidirectional ports on pic18f2x31 devices, 5 bidirectional ports on pic18f4x31 devices). all other features for devices in this family are identical. these are summarized in table 1-1. the pinouts for all devices are listed in table 1-2 and table 1-3. table 1-1: device features features pic18f2331 PIC18F2431 pic18f4331 pic18f4431 operating frequency dc ? 40 mhz dc ? 40 mhz dc ? 40 mhz dc ? 40 mhz program memory (bytes) 8192 16384 8192 16384 program memory (instructions) 4096 8192 4096 8192 data memory (bytes) 768 768 768 768 data eeprom memory (bytes) 256 256 256 256 interrupt sources 22 22 34 34 i/o ports ports a, b, c ports a, b, c ports a, b, c, d, e ports a, b, c, d, e timers 4 4 4 4 capture/compare/pwm modules 2 2 2 2 14-bit power control pwm (6 channels) (6 channels) (8 channels) (8 channels) motion feedback module (input capture/quadrature encoder interface) 1 qei or 3x ic 1 qei or 3x ic 1 qei or 3x ic 1 qei or 3x ic serial communications ssp, enhanced usart ssp, enhanced usart ssp, enhanced usart ssp, enhanced usart 10-bit high-speed analog-to-digital converter module 5 input channels 5 input channels 9 input channels 9 input channels resets (and delays) por, bor, reset instruction, stack full, stack underflow (pwrt, ost), mclr (optional), wdt por, bor, reset instruction, stack full, stack underflow (pwrt, ost), mclr (optional), wdt por, bor, reset instruction, stack full, stack underflow (pwrt, ost), mclr (optional), wdt por, bor, reset instruction, stack full, stack underflow (pwrt, ost), mclr (optional), wdt programmable low-voltage detect yes yes yes yes programmable brown-out reset yes yes yes yes instruction set 75 instructions 75 instructions 75 instructions 75 instructions packages 28-pin sdip 28-pin soic 28-pin sdip 28-pin soic 40-pin dip 44-pin tqfp 44-pin qfn 40-pin dip 44-pin tqfp 44-pin qfn
pic18f2331/2431/4331/4431 ds39616b-page 10 preliminary ? 2003 microchip technology inc. figure 1-1: pic18f2331/2431 block diagram power-up timer oscillator start-up timer power-on reset watchdog timer instruction decode & control osc1/clki osc2/clko mclr /v pp v dd , v ss porta portb portc ra4/an4/cap3/qeb rb0/pwm0 rb5/kbi1/pwm4/pgm rc0/t1oso/t1cki rc1/t1osi/ccp2/flta rc2/ccp1/fltb rc4/int1/sdi/sda rc5/int2/sck/scl rc6/tx/ck/ss rc7/rx/dt/sdo brown-out reset note 1: re3 input pin is only enabled when mclre fuse is programmed to ? 0 ?. 2: re3 is available only when mclr is disabled. eusart data ee synchronous timer0 timer1 timer2 serial port ra3/an3/v ref +/cap2/qea ra2/an2/v ref -/cap1/indx ra1/an1 ra0/an0 pcpwm timing generation 4x pll hs 10-bit adc rb1/pwm1 data latch data ram (768 bytes) address latch address<12> 12 bank0, f bsr fsr0 fsr1 fsr2 inc/dec logic decode 4 12 4 pch pcl pclath 8 31 level stack program counter prodl prodh 8 x 8 multiply w 8 bitop 8 8 alu<8> 8 address latch program memory data latch 20 21 21 16 8 8 8 table pointer<21> inc/dec logic 21 8 data bus<8> tablelatch 8 ir 12 3 romlatch timer5 porte ccp1 rb2/pwm2 rb3/pwm3 t1osi t1oso pclatu pcu osc2/clko/ra6 precision reference band gap rb4/kbi0/pwm5 rb6/kbi2/pgc rb7/kbi3/pgd rc3/t0cki/t5cki/int0 ccp2 mclr /v pp /re3 (1, 2) osc1/clki/ra7 power intrc osc av dd , av ss mode logic managed mfm
? 2003 microchip technology inc. preliminary ds39616b-page 11 pic18f2331/2431/4331/4431 figure 1-2: pic18f4331/4431 block diagram power-up timer oscillator start-up timer power-on reset watchdog timer instruction decode & control osc1/clki osc2/clko mclr /v pp v dd , v ss porta portb portc ra4/an4/cap3/qeb ra5/an5/lvdin rb0/pwm0 rb5/kbi1/pwm4/pgm (4) rc0/t1oso/t1cki rc1/t1osi/ccp2/flta (2) rc2/ccp1/fltb rc4/int1/sdi/sda (3) rc5/int2/sck/scl (3) rc6/tx/ck/ss rc7/rx/dt/sdo* brown-out reset note 1: re3 is available only when mclr is disabled. 2: rd4 is the alternate pin for flta . 3: rc3, rc4 and rc5 are alternate pins for t0cki/t5cki, sdi/sda, sck/scl respectively. 4: rd5 is the alternate pin for pwm4. eusart data ee synchronous timer0 timer1 timer2 serial port ra3/an3/v ref +/cap2/qea ra2/an2/v ref -/cap1/indx ra1/an1 ra0/an0 timing generation 4x pll hs 10-bit adc rb1/pwm1 data latch data ram (768 bytes) address latch address<12> 12 bank0, f bsr fsr0 fsr1 fsr2 inc/dec logic decode 4 12 4 pch pcl pclath 8 31 level stack program counter prodl prodh 8 x 8 multiply w 8 bitop 8 8 alu<8> 8 address latch program memory data latch 20 21 21 16 8 8 8 table pointer<21> inc/dec logic 21 8 data bus<8> tablelatch 8 ir 12 3 romlatch timer5 porte re0/an6 re1/an7 re2/an8 ccp1 rb2/pwm2 rb3/pwm3 t1osi t1oso pclatu pcu osc2/clko/ra6 precision reference band gap rb4/kbi0/pwm5 rb6/kbi2/pgc rb7/kbi3/pgd rc3/t0cki/t5cki/int0 (3) ccp2 mclr /v pp /re3 (1) osc1/clki/ra7 power intrc osc av dd , av ss mode logic managed portd rd0/it0cki/t5cki rd1/sdo rd2/sdi/sda rd3/sck/scl rd4/flta (2) rd5/pwm4 (4) rd6/pwm6 rd7/pwm7 pcpwm mfm
pic18f2331/2431/4331/4431 ds39616b-page 12 preliminary ? 2003 microchip technology inc. table 1-2: pic18f2331/2431 pinout i/o descriptions pin name pin number pin type buffer type description dip soic mclr /v pp /re3 mclr v pp re3 11 i p i st st master clear (input) or programming voltage (input). master clear (reset) input. this pin is an active-low reset to the device. high-voltage icsp programming enable pin. digital input. available only when mclr is disabled. osc1/clki/ra7 osc1 clki ra7 99 i i i/o st cmos ttl oscillator crystal or external clock input. oscillator crystal input or external clock source input. st buffer when configured in rc mode, cmos otherwise. external clock source input. always associated with pin function osc1. (see related osc1/clki, osc2/clko pins.) general purpose i/o pin. osc2/clko/ra6 osc2 clko ra6 10 10 o o i/o ? ? ttl oscillator crystal or clock output. oscillator crystal output. connects to crystal or resonator in crystal oscillator mode. in rc mode, osc2 pin outputs clko, which has 1/4 the frequency of osc1 and denotes the instruction cycle rate. general purpose i/o pin. porta is a bidirectional i/o port. ra0/an0 ra0 an0 22 i/o i ttl analog digital i/o. analog input 0. ra1/an1 ra1 an1 33 i/o i ttl analog digital i/o. analog input 1. ra2/an2/v ref -/cap1/indx ra2 an2 v ref - cap1 indx 44 i/o i i i i ttl analog analog st st digital i/o. analog input 2. a/d reference voltage (low) input. input capture pin 1. quadrature encoder interface index input pin. ra3/an3/v ref +/cap2/qea ra3 an3 v ref + cap2 qea 55 i/o i i i i ttl analog analog st st digital i/o. analog input 3. a/d reference voltage (high) input. input capture pin 2. quadrature encoder interface channel a input pin. ra4/an4/cap3/qeb ra4 an4 cap3 qeb 66 i/o i i i ttl analog st st digital i/o. analog input 4. input capture pin 3. quadrature encoder interface channel b input pin. legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open-drain (no diode to v dd )
? 2003 microchip technology inc. preliminary ds39616b-page 13 pic18f2331/2431/4331/4431 portb is a bidirectional i/o port. portb can be software programmed for internal weak pull-ups on all inputs. rb0/pwm0 rb0 pwm0 21 21 i/o o ttl ttl digital i/o. pwm output 0. rb1/pwm1 rb1 pwm1 22 22 i/o o ttl ttl digital i/o. pwm output 1. rb2/pwm2 rb2 pwm2 23 23 i/o o ttl ttl digital i/o. pwm output 2. rb3/pwm3 rb3 pwm3 24 24 i/o o ttl ttl digital i/o. pwm output 3. rb4/kbi0/pwm5 rb4 kbi0 pwm5 25 25 i/o i o ttl ttl ttl digital i/o. interrupt-on-change pin. pwm output 5. rb5/kbi1/pwm4/pgm rb5 kbi1 pwm4 pgm 26 26 i/o i o i/o ttl ttl ttl st digital i/o. interrupt-on-change pin. pwm output 4. low-voltage icsp programming entry pin. rb6/kbi2/pgc rb6 kbi2 pgc 27 27 i/o i i/o ttl ttl st digital i/o. interrupt-on-change pin. in-circuit debugger and icsp programming clock pin. rb7/kbi3/pgd rb7 kbi3 pgd 28 28 i/o i i/o ttl ttl st digital i/o. interrupt-on-change pin. in-circuit debugger and icsp programming data pin. table 1-2: pic18f2331/2431 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip soic legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open-drain (no diode to v dd )
pic18f2331/2431/4331/4431 ds39616b-page 14 preliminary ? 2003 microchip technology inc. portc is a bidirectional i/o port. rc0/t1oso/t1cki rc0 t1oso t1cki 11 11 i/o o i st ? st digital i/o. timer1 oscillator output. timer1 external clock input. rc1/t1osi/ccp2/flta rc1 t1osi ccp2 flta 12 12 i/o i i/o i st cmos st st digital i/o. timer1 oscillator input. capture2 input, compare2 output, pwm2 output. fault interrupt input pin. rc2/ccp1/fltb rc2 ccp1 fltb 13 13 i/o i/o i st st st digital i/o. capture1 input/compare1 output/pwm1 output. fault interrupt input pin,. rc3/t0cki/t5cki/int0 rc3 t0cki t5cki int0 14 14 i/o i i i st st st st digital i/o. timer0 alternate clock input. timer5 alternate clock input. external interrupt 0. rc4/int1/sdi/sda rc4 int1 sdi sda 15 15 i/o i i i/o st st st st digital i/o. external interrupt 1. spi? data in. i 2 c? data i/o. rc5/int2/sck/scl rc5 int2 sck scl 16 16 i/o i i/o i/o st st st st digital i/o. external interrupt 2. synchronous serial clock input/output for spi mode. synchronous serial clock input/output for i 2 c mode. rc6/tx/ck/ss rc6 tx ck ss 17 17 i/o o i/o i st ? st ttl digital i/o. usart asynchronous transmit. usart synchronous clock (see related rx/dt). spi slave select input. rc7/rx/dt/sdo rc7 rx dt sdo 18 18 i/o i i/o o st st st ? digital i/o. usart asynchronous receive. usart synchronous data (see related tx/ck). spi data out. v ss 8, 19 8, 19 p ? ground reference for logic and i/o pins. v dd 7, 20 7, 20 p ? positive supply for logic and i/o pins. table 1-2: pic18f2331/2431 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip soic legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open-drain (no diode to v dd )
? 2003 microchip technology inc. preliminary ds39616b-page 15 pic18f2331/2431/4331/4431 table 1-3: pic18f4331/4431 pinout i/o descriptions pin name pin number pin type buffer type description dip tqfp qfn mclr /v pp /re3 mclr v pp re3 11818 i p i st st master clear (input) or programming voltage (input). master clear (reset) input. this pin is an active-low. reset to the device. programming voltage input. digital input. available only when mclr is disabled. osc1/clki/ra7 osc1 clki ra7 13 30 32 i i i/o st cmos ttl oscillator crystal or external clock input. oscillator crystal input or external clock source input. st buffer when configured in rc mode, cmos otherwise. external clock source input. always associated with pin function osc1. (see related osc1/clki, osc2/clko pins.) general purpose i/o pin. osc2/clko/ra6 osc2 clko ra6 14 31 33 o o i/o ? ? ttl oscillator crystal or clock output. oscillator crystal output. connects to crystal or resonator in crystal oscillator mode. in rc mode, osc2 pin outputs clko, which has 1/4 the frequency of osc1 and denotes the instruction cycle rate. general purpose i/o pin. porta is a bidirectional i/o port. ra0/an0 ra0 an0 21919 i/o i ttl analog digital i/o. analog input 0. ra1/an1 ra1 an1 32020 i/o i ttl analog digital i/o. analog input 1. ra2/an2/v ref -/cap1/ indx ra2 an2 v ref - cap1 indx 42121 i/o i i i i ttl analog analog st st digital i/o. analog input 2. a/d reference voltage (low) input. input capture pin 1. quadrature encoder interface index input pin. ra3/an3/v ref +/ cap2/qea ra3 an3 v ref + cap2 qea 52222 i/o i i i i ttl analog analog st st digital i/o. analog input 3. a/d reference voltage (high) input. input capture pin 2. quadrature encoder interface channel a input pin. ra4/an4/cap3/qeb ra4 an4 cap3 qeb 62323 i/o i i i ttl analog st st digital i/o. analog input 4. input capture pin 3. quadrature encoder interface channel b input pin. ra5/an5/lvdin ra5 an5 lvdin 72424 i/o i i ttl analog analog digital i/o. analog input 5. low-voltage detect input. legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open-drain (no diode to v dd )
pic18f2331/2431/4331/4431 ds39616b-page 16 preliminary ? 2003 microchip technology inc. portb is a bidirectional i/o port. portb can be software programmed for internal weak pull-ups on all inputs. rb0/pwm0 rb0 pwm0 33 8 9 i/o o ttl ttl digital i/o. pwm output 0. rb1/pwm1 rb1 pwm1 34 9 10 i/o o ttl ttl digital i/o. pwm output 1. rb2/pwm2 rb2 pwm2 35 10 11 i/o o ttl ttl digital i/o. pwm output 2. rb3/pwm3 rb3 pwm3 36 11 12 i/o o ttl ttl digital i/o. pwm output 3. rb4/kbi0/pwm5 rb4 kbi0 pwm5 37 14 14 i/o i o ttl ttl ttl digital i/o. interrupt-on-change pin. pwm output 5. rb5/kbi1/pwm4/ pgm rb5 kbi1 pwm4 pgm 38 15 15 i/o i o i/o ttl ttl ttl st digital i/o. interrupt-on-change pin. pwm output 4. low-voltage icsp programming entry pin. rb6/kbi2/pgc rb6 kbi2 pgc 39 16 16 i/o i i/o ttl ttl st digital i/o. interrupt-on-change pin. in-circuit debugger and icsp programming clock pin. rb7/kbi3/pgd rb7 kbi3 pgd 40 17 17 i/o i i/o ttl ttl st digital i/o. interrupt-on-change pin. in-circuit debugger and icsp programming data pin. table 1-3: pic18f4331/4431 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip tqfp qfn legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open-drain (no diode to v dd )
? 2003 microchip technology inc. preliminary ds39616b-page 17 pic18f2331/2431/4331/4431 portc is a bidirectional i/o port. rc0/t1oso/t1cki rc0 t1oso t1cki 15 32 34 i/o o i st ? st digital i/o. timer1 oscillator output. timer1 external clock input. rc1/t1osi/ccp2/ flta rc1 t1osi ccp2 flta 16 35 35 i/o i i/o i st cmos st st digital i/o. timer1 oscillator input. capture2 input, compare2 output, pwm2 output. fault interrupt input pin. rc2/ccp1/fltb rc2 ccp1 fltb 17 36 36 i/o i/o i st st st digital i/o. capture1 input/compare1 output/pwm1 output. fault interrupt input pin. rc3/t0cki/t5cki/ int0 rc3 t0cki t5cki int0 18 37 37 i/o i i i st st st st digital i/o. timer0 alternate clock input. timer5 alternate clock input. external interrupt 0. rc4/int1/sdi/sda rc4 int1 sdi sda 23 42 42 i/o i i i/o st st st st digital i/o. external interrupt 1. spi data in. i 2 c data i/o. rc5/int2/sck/scl rc5 int2 sck scl 24 43 43 i/o i i/o i/o st st st st digital i/o. external interrupt 2. synchronous serial clock input/output for spi mode. synchronous serial clock input/output for i 2 c mode. rc6/tx/ck/ss rc6 tx ck ss 25 44 44 i/o o i/o i st ? st st digital i/o. usart asynchronous transmit. usart synchronous clock (see related rx/dt). spi slave select input. rc7/rx/dt/sdo rc7 rx dt sdo 26 1 1 i/o i i/o o st st st ? digital i/o. usart asynchronous receive. usart synchronous data (see related tx/ck). spi data out. table 1-3: pic18f4331/4431 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip tqfp qfn legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open-drain (no diode to v dd )
pic18f2331/2431/4331/4431 ds39616b-page 18 preliminary ? 2003 microchip technology inc. portd is a bidirectional i/o port, or a parallel slave port (psp) for interfacing to a microprocessor port. these pins have ttl input buffers when psp module is enabled. rd0/t0cki/t5cki rd0 t0cki t5cki 19 38 38 i/o i i st st st digital i/o. timer0 external clock input. timer5 input clock. rd1/sdo rd1 sdo 20 39 39 i/o o st ? digital i/o. spi data out. rd2/sdi/sda rd2 sdi sda 21 40 40 i/o i i/o st st st digital i/o. spi data in. i 2 c data i/o. rd3/sck/scl rd3 sck scl 22 41 41 i/o i/o i/o st st st digital i/o. synchronous serial clock input/output for spi mode. synchronous serial clock input/output for i 2 c mode. rd4/flta rd4 flta 27 2 2 i/o i st st digital i/o. fault interrupt input pin. rd5/pwm4 rd5 pwm4 28 3 3 i/o o st ttl digital i/o. pwm output 4. rd6/pwm6 rd6 pwm6 29 4 4 i/o o st ttl digital i/o. pwm output 6. rd7/pwm7 rd7 pwm7 30 5 5 i/o o st ttl digital i/o. pwm output 7. table 1-3: pic18f4331/4431 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip tqfp qfn legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open-drain (no diode to v dd )
? 2003 microchip technology inc. preliminary ds39616b-page 19 pic18f2331/2431/4331/4431 porte is a bidirectional i/o port. re0/an6 re0 an6 82525 i/o i st analog digital i/o. analog input 6. re1/an7 re1 an7 92626 i/o i st analog digital i/o. analog input 7. re2/an8 re2 an8 10 27 27 i/o i st analog digital i/o. analog input 8. v ss 12, 31 6, 29 6, 30, 31 p ? ground reference for logic and i/o pins. v dd 11, 32 7, 28 7, 8, 28, 29 p ? positive supply for logic and i/o pins. nc ? 12, 13, 33, 34 13 nc nc no connect table 1-3: pic18f4331/4431 pinout i/o descriptions (continued) pin name pin number pin type buffer type description dip tqfp qfn legend: ttl = ttl compatible input cmos = cmos compatible input or output st = schmitt trigger input with cmos levels i = input o = output p = power od = open-drain (no diode to v dd )
pic18f2331/2431/4331/4431 ds39616b-page 20 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 21 pic18f2331/2431/4331/4431 2.0 oscillator configurations 2.1 oscillator types the pic18f2331/2431/4331/4431 devices can be operated in 10 different oscillator modes. the user can program the configuration bits f osc 3:f osc 0 in config- uration register 1h to select one of these 10 modes: 1. lp low-power crystal 2. xt crystal/resonator 3. hs high-speed crystal/resonator 4. hspll high-speed crystal/resonator with pll enabled 5. rc external resistor/capacitor with f osc /4 output on ra6 6. rcio external resistor/capacitor with i/o on ra6 7. intio1 internal oscillator with f osc /4 output on ra6 and i/o on ra7 8. intio2 internal oscillator with i/o on ra6 and ra7 9. ec external clock with f osc /4 output 10. ecio external clock with i/o on ra6 2.2 crystal oscillator/ceramic resonators in xt, lp, hs or hspll oscillator modes, a crystal or ceramic resonator is connected to the osc1 and osc2 pins to establish oscillation. figure 2-1 shows the pin connections. the oscillator design requires the use of a parallel cut crystal. figure 2-1: crystal/ceramic resonator operation (xt, lp, hs or hspll configuration) table 2-1: capacitor selection for ceramic resonators note: use of a series cut crystal may give a frequency out of the crystal manufacturers? specifications. typical capacitor values used: mode freq osc1 osc2 xt 455 khz 2.0 mhz 4.0 mhz 56 pf 47 pf 33 pf 56 pf 47 pf 33 pf hs 8.0 mhz 16.0 mhz 27 pf 22 pf 27 pf 22 pf capacitor values are for design guidance only. these capacitors were tested with the resonators listed below for basic start-up and operation. these values are not optimized . different capacitor values may be required to produce acceptable oscillator operation. the user should test the performance of the oscillator over the expected v dd and temperature range for the application. see the notes on page 22 for additional information. resonators used: 455 khz 4.0 mhz 2.0 mhz 8.0 mhz 16.0 mhz note 1: see table 2-1 and table 2-2 for initial values of c1 and c2. 2: a series resistor (r s ) may be required for at strip cut crystals. 3: r f varies with the oscillator mode chosen. c1 (1) c2 (1) xtal osc2 osc1 r f (3) sleep to logic pic18fxxxx r s (2) internal
pic18f2331/2431/4331/4431 ds39616b-page 22 preliminary ? 2003 microchip technology inc. table 2-2: capacitor selection for crystal oscillator an external clock source may also be connected to the osc1 pin in the hs mode, as shown in figure 2-2. figure 2-2: external clock input operation (hs osc configuration) 2.3 hspll a phase locked loop (pll) circuit is provided as an option for users who wish to use a lower frequency crystal oscillator circuit, or to clock the device up to its highest rated frequency from a crystal oscillator. this may be useful for customers who are concerned with emi due to high-frequency crystals. the hspll mode makes use of the hs mode oscillator for frequencies up to 10 mhz. a pll then multiplies the oscillator output frequency by 4 to produce an internal clock frequency up to 40 mhz. the pll is enabled only when the oscillator configura- tion bits are programmed for hspll mode. if programmed for any other mode, the pll is not enabled. figure 2-3: pll block diagram osc type crystal freq typical capacitor values tested: c1 c2 lp 32 khz 33 pf 33 pf 200 khz 15 pf 15 pf xt 1 mhz 33 pf 33 pf 4 mhz 27 pf 27 pf hs 4 mhz 27 pf 27 pf 8 mhz 22 pf 22 pf 20 mhz 15 pf 15 pf capacitor values are for design guidance only. these capacitors were tested with the crystals listed below for basic start-up and operation. these values are not optimized. different capacitor values may be required to produce acceptable oscillator operation. the user should test the performance of the oscillator over the expected v dd and temperature range for the application. see the notes following this table for additional information. crystals used: 32 khz 4 mhz 200 khz 8 mhz 1 mhz 20 mhz note 1: higher capacitance increases the stability of oscillator, but also increases the start- up time. 2: when operating below 3v v dd , or when using certain ceramic resonators at any voltage, it may be necessary to use the hs mode or switch to a crystal oscillator. 3: since each resonator/crystal has its own characteristics, the user should consult the resonator/crystal manufacturer for appropriate values of external components. 4: rs may be required to avoid overdriving crystals with low drive level specification. 5: always verify oscillator performance over the v dd and temperature range that is expected for the application. osc1 osc2 open clock from ext. system pic18fxxxx (hs mode) mux vco loop filter crystal osc osc2 osc1 pll enable f in f out sysclk phase comparator hs osc enable 4 (from configuration register 1h) hs mode
? 2003 microchip technology inc. preliminary ds39616b-page 23 pic18f2331/2431/4331/4431 2.4 external clock input the ec and ecio oscillator modes require an external clock source to be connected to the osc1 pin. there is no oscillator start-up time required after a power-on reset or after an exit from sleep mode. in the ec oscillator mode, the oscillator frequency divided by 4 is available on the osc2 pin. this signal may be used for test purposes or to synchronize other logic. figure 2-4 shows the pin connections for the ec oscillator mode. figure 2-4: external clock input operation (ec configuration) the ecio oscillator mode functions like the ec mode, except that the osc2 pin becomes an additional general purpose i/o pin. the i/o pin becomes bit 6 of porta (ra6). figure 2-5 shows the pin connections for the ecio oscillator mode. figure 2-5: external clock input operation (ecio configuration) 2.5 rc oscillator for timing insensitive applications, the ?rc? and ?rcio? device options offer additional cost savings. the rc oscillator frequency is a function of the supply voltage, the resistor (r ext ) and capacitor (c ext ) values and the operating temperature. in addition to this, the oscillator frequency will vary from unit to unit due to normal manufacturing variation. furthermore, the difference in lead frame capacitance between package types will also affect the oscillation frequency, especially for low c ext values. the user also needs to take into account variation due to tolerance of external r and c components used. figure 2-6 shows how the r/c combination is connected. in the rc oscillator mode, the oscillator frequency divided by 4 is available on the osc2 pin. this signal may be used for test purposes or to synchronize other logic. figure 2-6: rc oscillator mode the rcio oscillator mode (figure 2-7) functions like the rc mode, except that the osc2 pin becomes an additional general purpose i/o pin. the i/o pin becomes bit 6 of porta (ra6). figure 2-7: rcio oscillator mode osc1/clki osc2/clko f osc /4 clock from ext. system pic18fxxxx osc1/clki i/o (osc2) ra6 clock from ext. system pic18fxxxx osc2/clko c ext r ext pic18fxxxx osc1 f osc /4 internal clock v dd v ss recommended values: 3 k ? r ext 100 k ? c ext > 20 pf c ext r ext pic18fxxxx osc1 internal clock v dd v ss recommended values: 3 k ? r ext 100 k ? c ext > 20 pf i/o (osc2) ra6
pic18f2331/2431/4331/4431 ds39616b-page 24 preliminary ? 2003 microchip technology inc. 2.6 internal oscillator block the pic18f2331/2431/4331/4431 devices include an internal oscillator block, which generates two different clock signals; either can be used as the system?s clock source. this can eliminate the need for external oscillator circuits on the osc1 and/or osc2 pins. the main output (intosc) is an 8 mhz clock source, which can be used to directly drive the system clock. it also drives a postscaler, which can provide a range of clock frequencies from 125 khz to 4 mhz. the intosc output is enabled when a system clock frequency from 125 khz to 8 mhz is selected. the other clock source is the internal rc oscillator (intrc), which provides a 31 khz output. the intrc oscillator is enabled by selecting the internal oscillator block as the system clock source, or when any of the following are enabled:  power-up timer  fail-safe clock monitor  watchdog timer  two-speed start-up these features are discussed in greater detail in section 22.0 ?special features of the cpu? . the clock source frequency (intosc direct, intrc direct or intosc postscaler) is selected by configuring the ircf bits of the osccon register (register 2-2). 2.6.1 intio modes using the internal oscillator as the clock source can eliminate the need for up to two external oscillator pins, which can then be used for digital i/o. two distinct configurations are available:  in intio1 mode, the osc2 pin outputs f osc /4, while osc1 functions as ra7 for digital input and output.  in intio2 mode, osc1 functions as ra7 and osc2 functions as ra6, both for digital input and output. 2.6.2 intrc output frequency the internal oscillator block is calibrated at the factory to produce an intosc output frequency of 8.0 mhz. this changes the frequency of the intrc source from its nominal 31.25 khz. peripherals and features that depend on the intrc source will be affected by this shift in frequency. 2.6.3 osctune register the internal oscillator?s output has been calibrated at the factory, but can be adjusted in the user's applica- tion. this is done by writing to the osctune register (register 2-1). the tuning sensitivity is constant throughout the tuning range. when the osctune register is modified, the intosc and intrc frequencies will begin shifting to the new frequency. the intrc clock will reach the new fre- quency within 8 clock cycles (approximately 8*32 s = 256 s). the intosc clock will stabilize within 1 ms. code execution continues during this shift. there is no indication that the shift has occurred. oper- ation of features that depend on the intrc clock source frequency, such as the wdt, fail-safe clock monitor and peripherals, will also be affected by the change in frequency.
? 2003 microchip technology inc. preliminary ds39616b-page 25 pic18f2331/2431/4331/4431 register 2-1: osctune: oscillator tuning register u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? tun5 tun4 tun3 tun2 tun1 tun0 bit 7 bit 0 bit 7, 6 unimplemented: read as ? 0 ? bit 5-0 tun<5:0>: frequency tuning bits 011111 = maximum frequency     000001 000000 = center frequency. oscillator module is running at the calibrated frequency. 111111     100000 = minimum frequency legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 26 preliminary ? 2003 microchip technology inc. 2.7 clock sources and oscillator switching like previous pic18 devices, the pic18f2331/2431/ 4331/4431 devices include a feature that allows the system clock source to be switched from the main oscillator to an alternate low frequency clock source. pic18f2331/2431/4331/4431 devices offer two alter- nate clock sources. when enabled, these give addi- tional options for switching to the various power- managed operating modes. essentially, there are three clock sources for these devices:  primary oscillators  secondary oscillators  internal oscillator block the primary oscillators include the external crystal and resonator modes, the external rc modes, the external clock modes and the internal oscillator block. the particular mode is defined on por by the contents of configuration register 1h. the details of these modes are covered earlier in this chapter. the secondary oscillators are those external sources not connected to the osc1 or osc2 pins. these sources may continue to operate even after the controller is placed in a power-managed mode. pic18f2331/2431/4331/4431 devices offer only the timer1 oscillator as a secondary oscillator. this oscillator, in all power-managed modes, is often the time base for functions such as a real-time clock. most often, a 32.768 khz watch crystal is connected between the rc0/t1oso and rc1/t1osi pins. like the lp mode oscillator circuit, loading capacitors are also connected from each pin to ground. the timer1 oscillator is discussed in greater detail in section 12.2 ?timer1 oscillator? . in addition to being a primary clock source, the internal oscillator block is available as a power-managed mode clock source. the intrc source is also used as the clock source for several special features, such as the wdt and fail-safe clock monitor. the clock sources for the pic18f2331/2431/4331/ 4431 devices are shown in figure 2-8. see section 12.0 ?timer1 module? for further details of the timer1 oscillator. see section 22.1 ?configura- tion bits? for configuration register details. 2.7.1 oscillator control register the osccon register (register 2-2) controls several aspects of the system clock?s operation, both in full power operation and in power-managed modes. the system clock select bits, scs1:scs0, select the clock source that is used when the device is operating in power-managed modes. the available clock sources are the primary clock (defined in configuration register 1h), the secondary clock (timer1 oscillator) and the internal oscillator block. the clock selection has no effect until a sleep instruction is executed and the device enters a power-managed mode of operation. the scs bits are cleared on all forms of reset. the internal oscillator select bits, ircf2:ircf0, select the frequency output of the internal oscillator block that is used to drive the system clock. the choices are the intrc source, the intosc source (8 mhz) or one of the six frequencies derived from the intosc postscaler (125 khz to 4 mhz). if the internal oscillator block is supplying the system clock, changing the states of these bits will have an immediate change on the internal oscillator?s output. the osts, iofs and t1run bits indicate which clock source is currently providing the system clock. the osts indicates that the oscillator start-up timer has timed out, and the primary clock is providing the system clock in primary clock modes. the iofs bit indicates when the internal oscillator block has stabilized, and is providing the system clock in rc clock modes. the t1run bit (t1con<6>) indicates when the timer1 oscillator is providing the system clock in secondary clock modes. in power-managed modes, only one of these three bits will be set at any time. if none of these bits are set, the intrc is providing the system clock, or the internal oscillator block has just started and is not yet stable. the idlen bit controls the selective shut down of the controller?s cpu in power-managed modes. the use of these bits is discussed in more detail in section 3.0 ?power-managed modes? note 1: the timer1 oscillator must be enabled to select the secondary clock source. the timer1 oscillator is enabled by setting the t1oscen bit in the timer1 control regis- ter (t1con<3>). if the timer1 oscillator is not enabled, then any attempt to select a secondary clock source when execut- ing a sleep instruction will be ignored. 2: it is recommended that the timer1 oscil- lator be operating and stable before exe- cuting the sleep instruction, or a very long delay may occur while the timer1 oscillator starts.
? 2003 microchip technology inc. preliminary ds39616b-page 27 pic18f2331/2431/4331/4431 figure 2-8: pic18f2331/2431/4331/4431 clock diagram pic18f2331/2431/4331/4431 4 x pll c4nfig1h <3:0> secondary oscillator t1oscen enable oscillator t1oso t1osi clock source option for other modules osc1 osc2 sleep primary oscillator hspll lp, xt, hs, rc, ec t1osc cpu peripherals idlen postscaler mux mux 8 mhz 4 mhz 2 mhz 1 mhz 500 khz 125 khz 250 khz osccon<6:4> 111 110 101 100 011 010 001 000 31 khz intrc source internal oscillator block wdt, fscm 8 mhz internal oscillator (intosc) osccon<6:4> clock control osccon<1:0>
pic18f2331/2431/4331/4431 ds39616b-page 28 preliminary ? 2003 microchip technology inc. register 2-2: osccon register 2.7.2 oscillator transitions the pic18f2331/2431/4331/4431 devices contain circuitry to prevent clocking ?glitches? when switching between clock sources. a short pause in the system clock occurs during the clock switch. the length of this pause is between 8 and 9 clock periods of the new clock source. this ensures that the new clock source is stable and that its pulse width will not be less than the shortest pulse width of the two clock sources. clock transitions are discussed in greater detail in section 3.1.2 ?entering power-managed modes? . r/w-0 r/w-0 r/w-0 r/w-0 r (1) r-0 r/w-0 r/w-0 idlen ircf2 ircf1 ircf0 osts iofs scs1 scs0 bit 7 bit 0 bit 7 idlen: idle enable bit 1 = idle mode enabled; cpu core is not clocked in power-managed modes 0 = run mode enabled; cpu core is clocked in power-managed modes bit 6-4 ircf2:ircf0: internal oscillator frequency select bits 111 = 8 mhz (8 mhz source drives clock directly) 110 = 4 mhz 101 = 2 mhz 100 = 1 mhz 011 = 500 khz 010 = 250 khz 001 = 125 khz 000 = 31 khz (intrc source drives clock directly) bit 3 osts: oscillator start-up time-out status bit 1 = oscillator start-up time-out timer ha s expired; primary oscillator is running 0 = oscillator start-up time-out timer is running; primary oscillator is not ready bit 2 iofs: intosc frequency stable bit 1 = intosc frequency is stable 0 = intosc frequency is not stable bit 1-0 scs1:scs0: system clock select bits 1x = internal oscillator block (rc modes) 01 = timer1 oscillator (secondary modes) 00 = primary oscillator (sleep and pri_idle modes) note 1: depends on state of the ieso bit in configuration register 1h. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 29 pic18f2331/2431/4331/4431 2.8 effects of power-managed modes on the various clock sources when the device executes a sleep instruction, the system is switched to one of the power-managed modes, depending on the state of the idlen and scs1:scs0 bits of the osccon register. see section 3.0 ?power-managed modes? for details. when pri_idle mode is selected, the designated primary oscillator continues to run without interruption. for all other power-managed modes, the oscillator using the osc1 pin is disabled. the osc1 pin (and osc2 pin, if used by the oscillator) will stop oscillating. in secondary clock modes (sec_run and sec_idle), the timer1 oscillator is operating and providing the system clock. the timer1 oscillator may also run in all power-managed modes if required to clock timer1. in internal oscillator modes (rc_run and rc_idle), the internal oscillator block provides the system clock source. the intrc output can be used directly to provide the system clock, and may be enabled to support various special features, regardless of the power-managed mode (see sections 22.2 through 22.4 ). the intosc output at 8 mhz may be used directly to clock the system, or may be divided down first. the intosc output is disabled if the system clock is provided directly from the intrc output. if the sleep mode is selected, all clock sources are stopped. since all the transistor switching currents have been stopped, sleep mode achieves the lowest current consumption of the device (only leakage currents). enabling any on-chip feature that will operate during sleep will increase the current consumed during sleep. the intrc is required to support wdt operation. the timer1 oscillator may be operating to support a real- time clock. other features may be operating that do not require a system clock source (i.e., ssp slave, psp, intn pins, a/d conversions and others). 2.9 power-up delays power-up delays are controlled by two timers, so that no external reset circuitry is required for most applications. the delays ensure that the device is kept in reset until the device power supply is stable under normal circumstances, and the primary clock is operating and stable. for additional information on power-up delays, see sections 4.1 through 4.5 . the first timer is the power-up timer (pwrt), which provides a fixed delay on power-up (parameter 33 , table 25-8), if enabled, in configuration register 2l. the second timer is the oscillator start-up timer (ost), intended to keep the chip in reset until the crys- tal oscillator is stable (lp, xt and hs modes). the ost does this by counting 1024 oscillator cycles before allowing the oscillator to clock the device. when the hspll oscillator mode is selected, the device is kept in reset for an additional 2 ms, following the hs mode ost delay, so the pll can lock to the incoming clock frequency. there is a delay of 5 to 10 s following por, while the controller becomes ready to execute instructions. this delay runs concurrently with any other delays. this may be the only delay that occurs when any of the ec, rc or intio modes are used as the primary clock source. table 2-3: osc1 and osc2 pin states in sleep mode osc mode osc1 pin osc2 pin rc, intio1 floating, external resistor should pull high at logic low (clock/4 output) rcio, intio2 floating, external resistor should pull high configured as porta, bit 6 ecio floating, pulled by external clock configured as porta, bit 6 ec floating, pulled by external clock at logic low (clock/4 output) lp, xt, and hs feedback inverter disabled, at quiescent voltage level feedback inverter disabled, at quiescent voltage level note: see table 4-1 in the section 4.0 ?reset? , for time-outs due to sleep and mclr reset.
pic18f2331/2431/4331/4431 ds39616b-page 30 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 31 pic18f2331/2431/4331/4431 3.0 power-managed modes the pic18f2331/2431/4331/4431 devices offer a total of six operating modes for more efficient power management (see table 3-1). these operating modes provide a variety of options for selective power conservation in applications where resources may be limited (i.e., battery-powered devices). there are three categories of power-managed modes:  sleep mode  idle modes  run modes these categories define which portions of the device are clocked and sometimes, what speed. the run and idle modes may use any of the three available clock sources (primary, secondary or intosc multiplexer); the sleep mode does not use a clock source. the clock switching feature offered in other pic18 devices (i.e., using the timer1 oscillator in place of the primary oscillator), and the sleep mode offered by all picmicro ? devices (where all system clocks are stopped) are both offered in the pic18f2331/2431/ 4331/4431 devices (sec_run and sleep modes, respectively). however, additional power-managed modes are available that allow the user greater flexibil- ity in determining what portions of the device are oper- ating. the power-managed modes are event driven; that is, some specific event must occur for the device to enter or (more particularly) exit these operating modes. for pic18f2331/2431/4331/4431 devices, the power- managed modes are invoked by using the existing sleep instruction. all modes exit to pri_run mode when triggered by an interrupt, a reset or a wdt time- out (pri_run mode is the normal full power execution mode; the cpu and peripherals are clocked by the pri- mary oscillator source). in addition, power-managed run modes may also exit to sleep mode or their corresponding idle mode. 3.1 selecting power-managed modes selecting a power-managed mode requires deciding if the cpu is to be clocked or not, and selecting a clock source. the idlen bit controls cpu clocking, while the sc1:scs0 bits select a clock source. the individual modes, bit settings, clock sources and affected modules are summarized in table 3-1. 3.1.1 clock sources the clock source is selected by setting the scs bits of the osccon register. three clock sources are avail- able for use in power-managed idle modes: the primary clock (as configured in configuration register 1h), the secondary clock (timer1 oscillator), and the internal oscillator block. the secondary and internal oscillator block sources are available for the power-managed modes (pri_run mode is the normal full power exe- cution mode; the cpu and peripherals are clocked by the primary oscillator source). table 3-1: power-managed modes mode osccon bits module clocking available clock and oscillator source idlen <7> scs1:scs0 <1:0> cpu peripherals sleep 000 off off none ? all clocks are disabled pri_run 000 clocked clocked primary ? lp, xt , hs, hspll, rc, ec, intrc (1) this is the normal full power execution mode. sec_run 001 clocked clocked secondary ? timer1 oscillator rc_run 01x clocked clocked internal oscillator block (1) pri_idle 100 off clocked primary ? lp, xt, hs, hspll, rc, ec sec_idle 101 off clocked secondary ? timer1 oscillator rc_idle 11x off clocked internal oscillator block (1) note 1: includes intosc and intosc postsc aler, as well as the intrc source.
pic18f2331/2431/4331/4431 ds39616b-page 32 preliminary ? 2003 microchip technology inc. 3.1.2 entering power-managed modes in general, entry, exit and switching between power- managed clock sources requires clock source switch- ing. in each case, the sequence of events is the same. any change in the power-managed mode begins with loading the osccon register and executing a sleep instruction. the scs1:scs0 bits select one of three power-managed clock sources; the primary clock (as defined in configuration register 1h), the secondary clock (the timer1 oscillator) and the internal oscillator block (used in rc modes). modifying the scs bits will have no effect until a sleep instruction is executed. entry to the power-managed mode is triggered by the execution of a sleep instruction. figure 3-5 shows how the system is clocked while switching from the primary clock to the timer1 oscilla- tor. when the sleep instruction is executed, clocks to the device are stopped at the beginning of the next instruction cycle. eight clock cycles from the new clock source are counted to synchronize with the new clock source. after eight clock pulses from the new clock source are counted, clocks from the new clock source resume clocking the system. the actual length of the pause is between eight and nine clock periods from the new clock source. this ensures that the new clock source is stable and that its pulse width will not be less than the shortest pulse width of the two clock sources. three bits indicate the current clock source: osts and iofs in the osccon register, and t1run in the t1con register. only one of these bits will be set while in a power-managed mode other than pri_run. when the osts bit is set, the primary clock is providing the system clock. when the iofs bit is set, the intosc output is providing a stable 8 mhz clock source and is providing the system clock. when the t1run bit is set, the timer1 oscillator is providing the system clock. if none of these bits are set, then either the intrc clock source is clocking the system, or the intosc source is not yet stable. if the internal oscillator block is configured as the pri- mary clock source in configuration register 1h, then both the osts and iofs bits may be set when in pri_run or pri_idle modes. this indicates that the primary clock (intosc output) is generating a stable 8 mhz output. entering an rc power-managed mode (same frequency) would clear the osts bit. 3.1.3 multiple sleep commands the power-managed mode that is invoked with the sleep instruction is determined by the settings of the idlen and scs bits at the time the instruction is exe- cuted. if another sleep instruction is executed, the device will enter the power-managed mode specified by these same bits at that time. if the bits have changed, the device will enter the new power-managed mode specified by the new bit settings. 3.1.4 comparisons between run and idle modes clock source selection for the run modes is identical to the corresponding idle modes. when a sleep instruc- tion is executed, the scs bits in the osccon register are used to switch to a different clock source. as a result, if there is a change of clock source at the time a sleep instruction is executed, a clock switch will occur. in idle modes, the cpu is not clocked and is not run- ning. in run modes, the cpu is clocked and executing code. this difference modifies the operation of the wdt when it times out. in idle modes, a wdt time-out results in a wake from power-managed modes. in run modes, a wdt time-out results in a wdt reset (see table 3-2). during a wake-up from an idle mode, the cpu starts executing code by entering the corresponding run mode, until the primary clock becomes ready. when the primary clock becomes ready, the clock source is auto- matically switched to the primary clock. the idlen and scs bits are unchanged during and after the wake-up. figure 3-2 shows how the system is clocked during the clock source switch. the example assumes the device was in sec_idle or sec_run mode when a wake is triggered (the primary clock was configured in hspll mode). note 1: caution should be used when modifying a single ircf bit. if v dd is less than 3v, it is possible to select a higher clock speed than is supported by the low v dd . improper device operation may result if the v dd /f osc specifications are violated. 2: executing a sleep instruction does not necessarily place the device into sleep mode; executing a sleep instruction is simply a trigger to place the controller into a power-managed mode selected by the osccon register, one of which is sleep mode.
? 2003 microchip technology inc. preliminary ds39616b-page 33 pic18f2331/2431/4331/4431 3.2 sleep mode the power-managed sleep mode in the pic18f2331/ 2431/4331/4431 devices is identical to that offered in all other picmicro ? controllers. it is entered by clearing the idlen and scs1:scs0 bits (this is the reset state), and executing the sleep instruction. this shuts down the primary oscillator and the osts bit is cleared (see figure 3-1). when a wake event occurs in sleep mode (by interrupt, reset, or wdt time-out), the system will not be clocked until the primary clock source becomes ready (see figure 3-2), or it will be clocked from the internal oscil- lator block if either the two-speed start-up or the fail- safe clock monitor are enabled (see section 22.0 ?special features of the cpu? ). in either case, the osts bit is set when the primary clock provides the system clocks. the idlen and scs bits are not affected by the wake-up. 3.3 idle modes the idlen bit allows the controller?s cpu to be selec- tively shut down while the peripherals continue to oper- ate. clearing idlen allows the cpu to be clocked. setting idlen disables clocks to the cpu, effectively stopping program execution (see register 2-2). the peripherals continue to be clocked regardless of the setting of the idlen bit. there is one exception to how the idlen bit functions. when all the low-power osccon bits are cleared (idlen:scs1:scs0 = 000 ), the device enters sleep mode upon the execution of the sleep instruction. this is both the reset state of the osccon register and the setting that selects sleep mode. this maintains com- patibility with other picmicro devices that do not offer power-managed modes. if the idle enable bit, idlen (osccon<7>), is set to a ? 1 ? when a sleep instruction is executed, the peripherals will be clocked from the clock source selected using the scs1:scs0 bits; however, the cpu will not be clocked. since the cpu is not executing instructions, the only exits from any of the idle modes are by interrupt, wdt time-out or a reset. when a wake event occurs, cpu execution is delayed approximately 10 s while it becomes ready to execute code. when the cpu begins executing code, it is clocked by the same clock source as was selected in the power-managed mode (i.e., when waking from rc_idle mode, the internal oscillator block will clock the cpu and peripherals until the primary clock source becomes ready ? this is essentially rc_run mode). this continues until the primary clock source becomes ready. when the primary clock becomes ready, the osts bit is set, and the system clock source is switched to the primary clock (see figure 3-4). the idlen and scs bits are not affected by the wake-up. while in any idle mode or the sleep mode, a wdt time- out will result in a wdt wake-up to full power operation. table 3-2: comparison between power-managed modes power managed mode cpu is clocked by ... wdt time-out causes a ... peripherals are clocked by ... clock during wake-up (while primary becomes ready) sleep not clocked (not running) wake-up not clocked none or intosc multiplexer if two-speed start-up or fail-safe clock monitor are enabled. any idle mode not clocked (not running) wake-up primary, secondary or intosc multiplexer unchanged from idle mode (cpu operates as in corresponding run mode). any run mode secondary, or intosc multiplexer reset secondary or intosc multiplexer unchanged from run mode.
pic18f2331/2431/4331/4431 ds39616b-page 34 preliminary ? 2003 microchip technology inc. figure 3-1: timing transition for entry to sleep mode figure 3-2: transition timing for wake from sleep (hspll) q4 q3 q2 osc1 peripheral sleep program q1 q1 counter clock cpu clock pc + 2 pc q3 q4 q1 q2 osc1 peripheral program pc pll clock q3 q4 output cpu clock q1 q2 q3 q4 q1 q2 clock counter pc + 8 pc + 6 q1 q2 q3 q4 wake event note 1: t ost = 1024 t osc ; t pll = 2 ms (approx). these intervals are not shown to scale. t ost (1) t pll (1) osts bit set pc + 4 pc + 2
? 2003 microchip technology inc. preliminary ds39616b-page 35 pic18f2331/2431/4331/4431 3.3.1 pri_idle mode this mode is unique among the three low-power idle modes, in that it does not disable the primary system clock. for timing sensitive applications, this allows for the fastest resumption of device operation with its more accurate primary clock source, since the clock source does not have to ?warm up? or transition from another oscillator. pri_idle mode is entered by setting the idlen bit, clearing the scs bits, and executing a sleep instruc- tion. although the cpu is disabled, the peripherals continue to be clocked from the primary clock source specified in configuration register 1h. the osts bit remains set in pri_idle mode (see figure 3-3). when a wake event occurs, the cpu is clocked from the primary clock source. a delay of approximately 10 s is required between the wake event and when code exe- cution starts. this is required to allow the cpu to become ready to execute instructions. after the wake- up, the osts bit remains set. the idlen and scs bits are not affected by the wake-up (see figure 3-4). figure 3-3: transition timing to pri_idle mode figure 3-4: transition timing for wake from pri_idle mode q1 peripheral program pc pc + 2 osc1 q3 q4 q1 cpu clock clock counter q2 osc1 peripheral program pc cpu clock pc + 2 q1 q3 q4 clock counter q2 wake event cpu start-up delay
pic18f2331/2431/4331/4431 ds39616b-page 36 preliminary ? 2003 microchip technology inc. 3.3.2 sec_idle mode in sec_idle mode, the cpu is disabled, but the peripherals continue to be clocked from the timer1 oscillator. this mode is entered by setting the idle bit, modifying to scs1:scs0 = 01 , and executing a sleep instruction. when the clock source is switched (see figure 3-5) to the timer1 oscillator, the primary oscilla- tor is shut down, the osts bit is cleared and the t1run bit is set. when a wake event occurs, the peripherals continue to be clocked from the timer1 oscillator. after a 10 s delay following the wake event, the cpu begins execut- ing code, being clocked by the timer1 oscillator. the microcontroller operates in sec_run mode until the primary clock becomes ready. when the primary clock becomes ready, a clock switch back to the primary clock occurs (see figure 3-6). when the clock switch is com- plete, the t1run bit is cleared, the osts bit is set and the primary clock is providing the system clock. the idlen and scs bits are not affected by the wake-up; the timer1 oscillator continues to run. figure 3-5: timing transition for entry to sec_idle mode figure 3-6: timing transition for wake from sec_run mode (hspll) note: the timer1 oscillator should already be running prior to entering sec_idle mode. if the t1oscen bit is not set when the sleep instruction is executed, a forced nop will be executed instead and entry to sec_idle mode will not occur. if the timer1 oscillator is enabled, but not yet running, peripheral clocks will be delayed until the oscillator has started; in such sit- uations, initial oscillator operation is far from stable and unpredictable operation may result. q4 q3 q2 osc1 peripheral program q1 t1osi q1 counter clock cpu clock pc + 2 pc 12345678 clock transition q1 q3 q4 osc1 peripheral program pc pc + 2 t1osi pll clock q1 pc + 6 q2 output q3 q4 q1 cpu clock pc + 4 clock counter q2 q2 q3 note 1: t ost = 1024 t osc ; t pll = 2 ms (approx). these intervals are not shown to scale. wake from interrupt event t ost (1) t pll (1) 12345678 clock transition osts bit set
? 2003 microchip technology inc. preliminary ds39616b-page 37 pic18f2331/2431/4331/4431 3.3.3 rc_idle mode in rc_idle mode, the cpu is disabled, but the periph- erals continue to be clocked from the internal oscillator block using the intosc multiplexer. this mode allows for controllable power conservation during idle periods. this mode is entered by setting the idlen bit, setting scs1 (scs0 is ignored), and executing a sleep instruction. the intosc multiplexer may be used to select a higher clock frequency by modifying the ircf bits before executing the sleep instruction. when the clock source is switched to the intosc multiplexer (see figure 3-7), the primary oscillator is shut down, and the osts bit is cleared. if the ircf bits are set to a non-zero value (thus enabling the intosc output), the iofs bit becomes set after the intosc output becomes stable, in about 1 ms. clocks to the peripherals continue while the intosc source stabilizes. if the ircf bits were previ- ously at a non-zero value before the sleep instruction was executed, and the intosc source was already stable, the iofs bit will remain set. if the ircf bits are all clear, the intosc output is not enabled and the iofs bit will remain clear; there will be no indication of the current clock source. when a wake event occurs, the peripherals continue to be clocked from the intosc multiplexer. after a 10 s delay following the wake event, the cpu begins exe- cuting code, being clocked by the intosc multiplexer. the microcontroller operates in rc_run mode until the primary clock becomes ready. when the primary clock becomes ready, a clock switch back to the pri- mary clock occurs (see figure 3-8). when the clock switch is complete, the iofs bit is cleared, the osts bit is set, and the primary clock is providing the system clock. the idlen and scs bits are not affected by the wake-up. the intrc source will continue to run if either the wdt or the fail-safe clock monitor is enabled. figure 3-7: timing transition to rc_idle mode figure 3-8: timing transition for wake from rc_run mode (rc_run to pri_run) q4 q3 q2 osc1 peripheral program q1 intrc q1 counter clock cpu clock pc + 2 pc 12345678 clock transition q1 q3 q4 osc1 peripheral program pc pc + 2 intosc pll clock q1 pc + 6 q2 output q3 q4 q1 cpu clock pc + 4 clock counter q2 q2 q3 note 1: t ost = 1024 t osc ; t pll = 2 ms (approx). these intervals are not shown to scale. wake from interrupt event t ost (1) t pll (1) 12345678 clock transition osts bit set multiplexer q4
pic18f2331/2431/4331/4431 ds39616b-page 38 preliminary ? 2003 microchip technology inc. 3.4 run modes if the idlen bit is clear when a sleep instruction is executed, the cpu and peripherals are both clocked from the source selected using the scs1:scs0 bits. while these operating modes may not afford the power conservation of idle or sleep modes, they do allow the device to continue executing instructions by using a lower frequency clock source. rc_run mode also offers the possibility of executing code at a frequency greater than the primary clock. wake-up from a power-managed run mode can be trig- gered by an interrupt, or any reset, to return to full power operation. as the cpu is executing code in run modes, several additional exits from run modes are possible. they include exit to sleep mode, exit to a cor- responding idle mode, and exit by executing a reset instruction. while the device is in any of the power- managed run modes, a wdt time-out will result in a wdt reset. 3.4.1 pri_run mode the pri_run mode is the normal full power execution mode. if the sleep instruction is never executed, the microcontroller operates in this mode (a sleep instruc- tion is executed to enter all other power-managed modes). all other power-managed modes exit to pri_run mode when an interrupt or wdt time-out occur. there is no entry to pri_run mode. the osts bit is set. the iofs bit may be set if the internal oscillator block is the primary clock source (see section 2.7.1 ?oscillator control register? ). 3.4.2 sec_run mode the sec_run mode is the compatible mode to the ?clock switching? feature offered in other pic18 devices. in this mode, the cpu and peripherals are clocked from the timer1 oscillator. this gives users the option of lower power consumption while still using a high accuracy clock source. sec_run mode is entered by clearing the idlen bit, setting scs1:scs0 = 01 , and executing a sleep instruction. the system clock source is switched to the timer1 oscillator (see figure 3-9), the primary oscilla- tor is shut down, the t1run bit (t1con<6>) is set and the osts bit is cleared. when a wake event occurs, the peripherals and cpu continue to be clocked from the timer1 oscillator while the primary clock is started. when the primary clock becomes ready, a clock switch back to the primary clock occurs (see figure 3-6). when the clock switch is com- plete, the t1run bit is cleared, the osts bit is set, and the primary clock is providing the system clock. the idlen and scs bits are not affected by the wake-up; the timer1 oscillator continues to run. firmware can force an exit from sec_run mode. by clearing the t1oscen bit (t1con<3>), an exit from sec_run back to normal full power operation is trig- gered. the timer1 oscillator will continue to run and provide the system clock even though the t1oscen bit is cleared. the primary clock is started. when the pri- mary clock becomes ready, a clock switch back to the primary clock occurs (see figure 3-6). when the clock switch is complete, the timer1 oscillator is disabled, the t1run bit is cleared, the osts bit is set and the pri- mary clock provides the system clock. the idlen and scs bits are not affected by the wake-up. figure 3-9: timing transition for entry to sec_run mode note: the timer1 oscillator should already be running prior to entering sec_run mode. if the t1oscen bit is not set when the sleep instruction is executed, a forced nop will be executed instead and entry to sec_idle mode will not occur. if the timer1 oscillator is enabled, but not yet running, system clocks will be delayed until the oscillator has started. in such situations, initial oscillator operation is far from stable and unpredictable operation may result. q4 q3 q2 osc1 peripheral program q1 t1osi q1 counter clock cpu clock pc + 2 pc 12345678 clock transition q4 q3 q2 q1 q3 q2 pc + 2
? 2003 microchip technology inc. preliminary ds39616b-page 39 pic18f2331/2431/4331/4431 3.4.3 rc_run mode in rc_run mode, the cpu and peripherals are clocked from the internal oscillator block using the intosc multiplexer, and the primary clock is shut down. when using the intrc source, this mode pro- vides the best power conservation of all the run modes, while still executing code. this mode works well for user applications that are not highly timing sensitive, or do not require high-speed clocks at all times. if the primary clock source is the internal oscillator block (either of the intio1 or intio2 oscillators), there are no distinguishable differences between pri_run and rc_run modes during execution. however, a clock switch delay will occur during entry to, and exit from, rc_run mode. therefore, if the primary clock source is the internal oscillator block, the use of rc_run mode is not recommended. this mode is entered by clearing the idlen bit, setting scs1 (scs0 is ignored) and executing a sleep instruction. the ircf bits may select the clock frequency before the sleep instruction is executed. when the clock source is switched to the intosc multiplexer (see figure 3-10), the primary oscillator is shut down and the osts bit is cleared. the ircf bits may be modified at any time to immedi- ately change the system clock speed. executing a sleep instruction is not required to select a new clock frequency from the intosc multiplexer. if the ircf bits are all clear, the intosc output is not enabled, and the iofs bit will remain clear; there will be no indication of the current clock source. the intrc source is providing the system clocks. if the ircf bits are changed from all clear (thus enabling the intosc output), the iofs bit becomes set after the intosc output becomes stable. clocks to the system continue while the intosc source stabilizes in approximately 1 ms. if the ircf bits were previously at a non-zero value before the sleep instruction was executed, and the intosc source was already stable, the iofs bit will remain set. when a wake event occurs, the system continues to be clocked from the intosc multiplexer while the primary clock is started. when the primary clock becomes ready, a clock switch to the primary clock occurs (see figure 3-8). when the clock switch is complete, the iofs bit is cleared, the osts bit is set and the primary clock provides the system clock. the idlen and scs bits are not affected by the wake-up. the intrc source will continue to run if either the wdt or the fail-safe clock monitor is enabled. figure 3-10: timing transition to rc_run mode note: caution should be used when modifying a single ircf bit. if v dd is less than 3v, it is possible to select a higher clock speed than is supported by the low v dd . improper device operation may result if the v dd /f osc specifications are violated. q3 q2 q1 osc1 peripheral program q4 intrc q4 counter clock cpu clock pc + 2 pc 12345678 clock transition q3 q2 q1 q4 q2 q1 q3 pc + 4
pic18f2331/2431/4331/4431 ds39616b-page 40 preliminary ? 2003 microchip technology inc. 3.4.4 exit to idle mode an exit from a power-managed run mode to its corre- sponding idle mode is executed by setting the idlen bit and executing a sleep instruction. the cpu is halted at the beginning of the instruction following the sleep instruction. there are no changes to any of the clock source status bits (osts, iofs, or t1run). while the cpu is halted, the peripherals continue to be clocked from the previously selected clock source. 3.4.5 exit to sleep mode an exit from a power-managed run mode to sleep mode is executed by clearing the idlen and scs1:scs0 bits and executing a sleep instruction. the code is no different than the method used to invoke sleep mode from the normal operating (full power) mode. the primary clock and internal oscillator block are dis- abled. the intrc will continue to operate if the wdt is enabled. the timer1 oscillator will continue to run, if enabled, in the t1con register. all clock source status bits are cleared (osts, iofs and t1run). 3.5 wake from power-managed modes an exit from any of the power-managed modes is trig- gered by an interrupt, a reset or a wdt time-out. this section discusses the triggers that cause exits from power-managed modes. the clocking subsystem actions are discussed in each of the power-managed modes (see sections 3.2 through 3.4 ). device behavior during low-power mode exits is summarized in table 3-3. 3.5.1 exit by interrupt any of the available interrupt sources can cause the device to exit a power-managed mode and resume full power operation. to enable this functionality, an inter- rupt source must be enabled by setting its enable bit in one of the intcon or pie registers. the exit sequence is initiated when the corresponding interrupt flag bit is set. on all exits from low-power mode by interrupt, code execution branches to the interrupt vector if the gie/gieh bit (intcon<7>) is set. otherwise, code execution continues or resumes without branching (see section 9.0 ?interrupts? ). note: if application code is timing sensitive, it should wait for the osts bit to become set before continuing. use the interval during the low-power exit sequence (before osts is set) to perform timing insensitive ?housekeeping? tasks.
? 2003 microchip technology inc. preliminary ds39616b-page 41 pic18f2331/2431/4331/4431 table 3-3: activity and exit delay on wake from sleep mode or any idle mode (by clock sources) clock in power- managed mode primary system clock power- managed mode exit delay clock ready status bit (osccon) activity during wake from power-managed mode exit by interrupt exit by reset primary system clock (pri_idle mode) lp, xt, hs 5-10 s (5) osts cpu and peripherals clocked by primary clock and executing instructions. not clocked, or two-speed start-up (if enabled) (3) . hspll ec, rc, intrc (1) ? intosc (2) iofs t1osc or intrc (1) lp, xt, hs ost osts cpu and peripherals clocked by selected power-managed mode clock and executing instructions until primary clock source becomes ready. hspll ost + 2 ms ec, rc, intrc (1) 5-10 s (5) ? intosc (2) 1ms (4) iofs intosc (2) lp, xt, hs ost osts hspll ost + 2 ms ec, rc, intrc (1) 5-10 s (5) ? intosc (2) none iofs sleep mode lp, xt, hs ost osts not clocked or two-speed start-up (if enabled) until primary clock source becomes ready (3) . hspll ost + 2 ms ec, rc, intrc (1) 5-10 s (5) ? intosc (2) 1ms (4) iofs note 1: in this instance, refers specifically to the intrc clock source. 2: includes both the intosc 8 mhz source and postscaler derived frequencies. 3: two-speed start-up is covered in greater detail in section 22.3 ?two-speed start-up? . 4: execution continues during the intosc stabilization period. 5: required delay when waking from sleep and all idle modes. this delay runs concurrently with any other required delays (see section 3.3 ?idle modes? ).
pic18f2331/2431/4331/4431 ds39616b-page 42 preliminary ? 2003 microchip technology inc. 3.5.2 exit by reset normally, the device is held in reset by the oscillator start-up timer (ost) until the primary clock (defined in configuration register 1h) becomes ready. at that time, the osts bit is set and the device begins executing code. code execution can begin before the primary clock becomes ready. if either the two-speed start-up (see section 22.3 ?two-speed start-up? ) or fail-safe clock monitor (see section 22.4 ?fail-safe clock monitor? ) are enabled in configuration register 1h, the device may begin execution as soon as the reset source has cleared. execution is clocked by the intosc multiplexer driven by the internal oscillator block. since the osccon register is cleared following all resets, the intrc clock source is selected. a higher speed clock may be selected by modifying the ircf bits in the osccon register. execution is clocked by the internal oscillator block until either the primary clock becomes ready, or a power-managed mode is entered before the primary clock becomes ready; the primary clock is then shut down. 3.5.3 exit by wdt time-out a wdt time-out will cause different actions depending on which power-managed mode the device is in when the time-out occurs. if the device is not executing code (all idle modes and sleep mode), the time-out will result in a wake from the power-managed mode (see section 3.2 ?sleep mode? through section 3.4 ?run modes? ). if the device is executing code (all run modes), the time-out will result in a wdt reset (see section 22.2 ?watchdog timer (wdt)? ). the wdt timer and postscaler are cleared by execut- ing a sleep or clrwdt instruction, the loss of a cur- rently selected clock source (if the fail-safe clock monitor is enabled), and modifying the ircf bits in the osccon register if the internal oscillator block is the system clock source. 3.5.4 exit without an oscillator start-up delay certain exits from power-managed modes do not invoke the ost at all. these are:  pri_idle mode where the primary clock source is not stopped; and  the primary clock source is not any of lp, xt, hs or hspll modes. in these cases, the primary clock source either does not require an oscillator start-up delay, since it is already running (pri_idle), or normally does not require an oscillator start-up delay (rc, ec, and intio oscillator modes). however, a fixed delay (approximately 10 s) following the wake event is required when leaving sleep and idle modes. this delay is required for the cpu to prepare for execution. instruction execution resumes on the first clock cycle following this delay. 3.6 intosc frequency drift the factory calibrates the internal oscillator block out- put (intosc) for 8 mhz. however, this frequency may drift as v dd or temperature changes, which can affect the controller operation in a variety of ways. it is possible to adjust the intosc frequency by modi- fying the value in the osctune register. this has the side effect that the intrc clock source frequency is also affected. however, the features that use the intrc source often do not require an exact frequency. these features include the fail-safe clock monitor, the watchdog timer and the rc_run/rc_idle modes when the intrc clock source is selected. being able to adjust the intosc requires knowing when an adjustment is required, in which direction it should be made, and in some cases, how large a change is needed. three examples follow, but other techniques may be used.
? 2003 microchip technology inc. preliminary ds39616b-page 43 pic18f2331/2431/4331/4431 3.6.1 example ? usart an adjustment may be indicated when the usart begins to generate framing errors, or receives data with errors while in asynchronous mode. framing errors indicate that the system clock frequency is too high ? try decrementing the value in the osctune register to reduce the system clock frequency. errors in data may suggest that the system clock speed is too low ? increment osctune. 3.6.2 example ? timers this technique compares system clock speed to some reference clock. two timers may be used; one timer is clocked by the peripheral clock, while the other is clocked by a fixed reference source, such as the timer1 oscillator. both timers are cleared, but the timer clocked by the reference generates interrupts. when an interrupt occurs, the internally clocked timer is read and both timers are cleared. if the internally clocked timer value is greater than expected, then the internal oscillator block is running too fast ? decrement osctune. 3.6.3 example ? ccp in capture mode a ccp module can use free running timer1, clocked by the internal oscillator block and an external event with a known period (i.e., ac power frequency). the time of the first event is captured in the ccprxh:ccprxl registers and is recorded for use later. when the second event causes a capture, the time of the first event is subtracted from the time of the second event. since the period of the external event is known, the time difference between events can be calculated. if the measured time is much greater than the calculated time, the internal oscillator block is running too fast ? decrement osctune. if the measured time is much less than the calculated time, the internal oscillator block is running too slow ? increment osctune.
pic18f2331/2431/4331/4431 ds39616b-page 44 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 45 pic18f2331/2431/4331/4431 4.0 reset the pic18f2331/2431/4331/4431 devices differenti- ate between various kinds of reset: a) power-on reset (por) b) mclr reset during normal operation c) mclr reset during sleep d) watchdog timer (wdt) reset (during execution) e) programmable brown-out reset (bor) f) reset instruction g) stack full reset h) stack underflow reset most registers are unaffected by a reset. their status is unknown on por and unchanged by all other resets. the other registers are forced to a ?reset state? depending on the type of reset that occurred. most registers are not affected by a wdt wake-up, since this is viewed as the resumption of normal operation. status bits from the rcon register, ri , to , pd , por and bor, are set or cleared differently in different reset situations, as indicated in table 4-2. these bits are used in software to determine the nature of the reset. see table 4-3 for a full description of the reset states of all registers. a simplified block diagram of the on-chip reset circuit is shown in figure 4-1. the enhanced mcu devices have a mclr noise filter in the mclr reset path. the filter will detect and ignore small pulses. the mclr pin is not driven low by any internal resets, including the wdt. the mclr input provided by the mclr pin can be dis- abled with the mclre bit in configuration register 3h (config3h<7>). see section 22.1 ?configuration bits? for more information. figure 4-1: simplified block diagram of on-chip reset circuit s r q external reset mclr v dd osc1 wdt time-out v dd rise detect ost/pwrt intrc (1) por pulse ost 10-bit ripple counter pwrt chip_reset 11-bit ripple counter enable ost (2) enable pwrt note 1: this is the intrc source from the internal oscillator bloc k, and is separate from the rc oscillator of the clki pin. 2: see table 4-1 for time-out situations. brown-out reset boren reset instruction stack pointer stack full/underflow reset sleep ( )_idle 1024 cycles 65.5 ms 32 s mclre
pic18f2331/2431/4331/4431 ds39616b-page 46 preliminary ? 2003 microchip technology inc. 4.1 power-on reset (por) a power-on reset pulse is generated on-chip when v dd rise is detected. to take advantage of the por cir- cuitry, just tie the mclr pin through a resistor (1k to 10 k ? ) to v dd . this will eliminate external rc compo- nents usually needed to create a power-on reset delay. a minimum rise rate for v dd is specified (parameter d004). for a slow rise time, see figure 4-2. when the device starts normal operation (i.e., exits the reset condition), device operating parameters (volt- age, frequency, temperature, etc.) must be met to ensure operation. if these conditions are not met, the device must be held in reset until the operating conditions are met. figure 4-2: external power-on reset circuit (for slow v dd power-up) 4.2 power-up timer (pwrt) the power-up timer (pwrt) of the pic18f2331/2431/ 4331/4431 devices is an 11-bit counter, which uses the intrc source as the clock input. this yields a count of 2048 x 32 s = 65.6 ms. while the pwrt is counting, the device is held in reset. the power-up time delay depends on the intrc clock and will vary from chip-to-chip due to temperature and process variation. see dc parameter #33 for details. the pwrt is enabled by clearing configuration bit pwrten . 4.3 oscillator start-up timer (ost) the oscillator start-up timer (ost) provides a 1024 oscillator cycle (from osc1 input) delay after the pwrt delay is over (parameter #33). this ensures that the crystal oscillator or resonator has started and stabilized. the ost time-out is invoked only for xt, lp, hs and hspll modes, and only on power-on reset or on exit from most power-managed modes. 4.4 pll lock time-out with the pll enabled in its pll mode, the time-out sequence following a power-on reset is slightly differ- ent from other oscillator modes. a portion of the power- up timer is used to provide a fixed time-out that is suf- ficient for the pll to lock to the main oscillator fre- quency. this pll lock time-out (t pll ) is typically 2 ms and follows the oscillator start-up time-out. 4.5 brown-out reset (bor) a configuration bit, boren, can disable (if clear/ programmed) or enable (if set) the brown-out reset cir- cuitry. if v dd falls below v bor (parameter d005) for greater than t bor (parameter #35), the brown-out situ- ation will reset the chip. a reset may not occur if v dd falls below v bor for less than t bor . the chip will remain in brown-out reset until v dd rises above v bor . if the power-up timer is enabled, it will be invoked after v dd rises above v bor ; it then will keep the chip in reset for an additional time delay t pwrt (parameter #33). if v dd drops below v bor while the power-up timer is running, the chip will go back into a brown-out reset and the power-up timer will be initialized. once v dd rises above v bor , the power-up timer will execute the additional time delay. enabling bor reset does not automatically enable the pwrt. 4.6 time-out sequence on power-up, the time-out sequence is as follows: first, after the por pulse has cleared, pwrt time-out is invoked (if enabled). then, the ost is activated. the total time-out will vary based on oscillator configuration and the status of the pwrt. for example, in rc mode with the pwrt disabled, there will be no time-out at all. figures 4-3 through 4-7 depict time-out sequences on power-up. since the time-outs occur from the por pulse, if mclr is kept low long enough, all time-outs will expire. bring- ing mclr high will begin execution immediately (figure 4-5). this is useful for testing purposes or to synchronize more than one pic18fxxxx device operating in parallel. table 4-2 shows the reset conditions for some special function registers, while table 4-3 shows the reset conditions for all the registers. note 1: external power-on reset circuit is required only if the v dd power-up slope is too slow. the diode d helps discharge the capacitor quickly when v dd powers down. 2: r < 40 k ? is recommended to make sure that the voltage drop across r does not violate the device?s electrical specifi- cation. 3: r1 1 k ? will limit any current flowing into mclr from external capacitor c, in the event of mclr /v pp pin breakdown, due to electrostatic discharge (esd) or electrical overstress (eos). c r1 r d v dd mclr pic18fxxxx v dd
? 2003 microchip technology inc. preliminary ds39616b-page 47 pic18f2331/2431/4331/4431 table 4-1: time-out in various situations register 4-1: rcon register bits and positions table 4-2: status bits, their significanc e and the initialization condition for rcon register oscillator configuration power-up (2) and brown-out exit from power-managed mode pwrten = 0 pwrten = 1 hspll 66 ms (1) + 1024 t osc + 2 ms (2) 1024 t osc + 2 ms (2) 1024 t osc + 2 ms (2) hs, xt, lp 66 ms (1) + 1024 t osc 1024 t osc 1024 t osc ec, ecio 66 ms (1) ?? rc, rcio 66 ms (1) ?? intio1, intio2 66 ms (1) ?? note 1: 66 ms (65.5 ms) is the nominal power-up timer (pwrt) delay. 2: 2 ms is the nominal time required for the 4x pll to lock. r/w-0 u-0 u-0 r/w-1 r-1 r-1 r/w-1 r/w-1 ipen ? ?ri to pd por bor bit 7 bit 0 note: refer to section 5.14 ?rcon register? for bit definitions. condition program counter rcon register ri to pd por bor stkful stkunf power-on reset 0000h 0--1 1100 1 1 1 0 0 0 0 reset instruction 0000h 0--0 uuuu 0 u u u u u u brown-out 0000h 0--1 11u- 1 1 1 u 0 u u mclr during power-managed run modes 0000h 0--u 1uuu u 1 u u u u u mclr during power-managed idle modes and sleep 0000h 0--u 10uu u 1 0 u u u u wdt time-out during full power or power-managed run 0000h 0--u 0uuu u 0 u u u u u mclr during full power execution 0000h 0--u uuuu u u u u u uu stack full reset (stvren = 1 ) 1u stack underflow reset (stvren = 1 ) u1 stack underflow error (not an actual reset, stvren = 0 ) 0000h u--u uuuu u u u u u u 1 wdt time-out during power- managed idle or sleep pc + 2 u--u 00uu u 0 0 u u u u interrupt exit from power-man- aged modes pc + 2 (1) u--u u0uu u u 0 u u u u legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?. note 1: when the wake-up is due to an interrupt and the gieh or giel bits are set, the pc is loaded with the interrupt vector (0x000008h or 0x000018h).
pic18f2331/2431/4331/4431 ds39616b-page 48 preliminary ? 2003 microchip technology inc. table 4-3: initialization co nditions for all registers register applicable devices power-on reset, brown-out reset mclr resets wdt reset reset instruction stack resets wake-up via wdt or interrupt tosu 2331 2431 4331 4431 ---0 0000 ---0 0000 ---0 uuuu (3) tosh 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu (3) tosl 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu (3) stkptr 2331 2431 4331 4431 00-0 0000 uu-0 0000 uu-u uuuu (3) pclatu 2331 2431 4331 4431 ---0 0000 ---0 0000 ---u uuuu pclath 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu pcl 2331 2431 4331 4431 0000 0000 0000 0000 pc + 2 (2) tblptru 2331 2431 4331 4431 --00 0000 --00 0000 --uu uuuu tblptrh 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu tblptrl 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu tablat 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu prodh 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu prodl 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu intcon 2331 2431 4331 4431 0000 000x 0000 000u uuuu uuuu (1) intcon2 2331 2431 4331 4431 1111 -1-1 1111 -1-1 uuuu -u-u (1) intcon3 2331 2431 4331 4431 11-0 0-00 11-0 0-00 uu-u u-uu (1) indf0 2331 2431 4331 4431 n/a n/a n/a postinc0 2331 2431 4331 4431 n/a n/a n/a postdec0 2331 2431 4331 4431 n/a n/a n/a preinc0 2331 2431 4331 4431 n/a n/a n/a plusw0 2331 2431 4331 4431 n/a n/a n/a fsr0h 2331 2431 4331 4431 ---- xxxx ---- uuuu ---- uuuu fsr0l 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu wreg 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu indf1 2331 2431 4331 4431 n/a n/a n/a postinc1 2331 2431 4331 4431 n/a n/a n/a postdec1 2331 2431 4331 4431 n/a n/a n/a preinc1 2331 2431 4331 4431 n/a n/a n/a plusw1 2331 2431 4331 4431 n/a n/a n/a legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hardware stack. 4: see table 4-2 for reset value for specific condition. 5: bits 6 and 7 of porta, lata and trisa are enabled, depending on the oscillator mode selected. when not enabled as porta pins, they are disabled and read ? 0 ?. 6: bit 3 of porte and late are enabled if mclr functionality is disabled. when not enabled as the porte pin, they are disabled and read as ? 0 ?. the 28-pin devices have only re3 on porte when mclr is disabled.
? 2003 microchip technology inc. preliminary ds39616b-page 49 pic18f2331/2431/4331/4431 fsr1h 2331 2431 4331 4431 ---- xxxx ---- uuuu ---- uuuu fsr1l 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu bsr 2331 2431 4331 4431 ---- 0000 ---- 0000 ---- uuuu indf2 2331 2431 4331 4431 n/a n/a n/a postinc2 2331 2431 4331 4431 n/a n/a n/a postdec2 2331 2431 4331 4431 n/a n/a n/a preinc2 2331 2431 4331 4431 n/a n/a n/a plusw2 2331 2431 4331 4431 n/a n/a n/a fsr2h 2331 2431 4331 4431 ---- xxxx ---- uuuu ---- uuuu fsr2l 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu status 2331 2431 4331 4431 ---x xxxx ---u uuuu ---u uuuu tmr0h 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu tmr0l 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu t0con 2331 2431 4331 4431 11-- 1111 11-- 1111 uu-- uuuu osccon 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu lvdcon 2331 2431 4331 4431 --00 0101 --00 0101 --uu uuuu wdtcon 2331 2431 4331 4431 ---- ---0 ---- ---0 ---- ---u rcon (4) 2331 2431 4331 4431 0--1 11q0 0--q qquu u--u qquu tmr1h 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu tmr1l 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu t1con 2331 2431 4331 4431 0000 0000 u0uu uuuu uuuu uuuu tmr2 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu pr2 2331 2431 4331 4431 1111 1111 1111 1111 1111 1111 t2con 2331 2431 4331 4431 -000 0000 -000 0000 -uuu uuuu sspbuf 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu sspadd 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu sspstat 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu sspcon 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu table 4-3: initialization conditions for all registers (continued) register applicable devices power-on reset, brown-out reset mclr resets wdt reset reset instruction stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hardware stack. 4: see table 4-2 for reset value for specific condition. 5: bits 6 and 7 of porta, lata and trisa are enabled, depending on the oscillator mode selected. when not enabled as porta pins, they are disabled and read ? 0 ?. 6: bit 3 of porte and late are enabled if mclr functionality is disabled. when not enabled as the porte pin, they are disabled and read as ? 0 ?. the 28-pin devices have only re3 on porte when mclr is disabled.
pic18f2331/2431/4331/4431 ds39616b-page 50 preliminary ? 2003 microchip technology inc. adresh 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu adresl 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu adcon0 2331 2431 4331 4431 --00 0000 --00 0000 --uu uuuu adcon1 2331 2431 4331 4431 00-0 1000 00-- 1000 uu-u uuuu adcon2 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu ccpr1h 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu ccpr1l 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu ccp1con 2331 2431 4331 4431 --00 0000 --00 0000 --uu uuuu ccpr2h 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu ccpr2l 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu ccp2con 2331 2431 4331 4431 --00 0000 --00 0000 --uu uuuu ansel0 2331 2431 4331 4431 1111 1111 1111 1111 uuuu uuuu ansel1 2331 2431 4331 4431 ---- ---0 ---- ---0 ---- ---u t5con 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu qeicon 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu spbrgh 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu spbrg 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu rcreg 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu txreg 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu txsta 2331 2431 4331 4431 0000 -010 0000 -010 uuuu -uuu rcsta 2331 2431 4331 4431 0000 000x 0000 000x uuuu uuuu baudctl 2331 2431 4331 4431 -1-1 0-00 -1-1 0-00 -u-u u-uu eeadr 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu eedata 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu eecon1 2331 2431 4331 4431 xx-0 x000 uu-0 u000 uu-0 u000 eecon2 2331 2431 4331 4431 0000 0000 0000 0000 0000 0000 ipr3 2331 2431 4331 4431 ---1 1111 ---1 1111 ---u uuuu pie3 2331 2431 4331 4431 ---0 0000 ---0 0000 ---u uuuu pir3 2331 2431 4331 4431 ---0 0000 ---0 0000 ---u uuuu table 4-3: initialization conditi ons for all registers (continued) register applicable devices power-on reset, brown-out reset mclr resets wdt reset reset instruction stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hardware stack. 4: see table 4-2 for reset value for specific condition. 5: bits 6 and 7 of porta, lata and trisa are enabled, depending on the oscillator mode selected. when not enabled as porta pins, they are disabled and read ? 0 ?. 6: bit 3 of porte and late are enabled if mclr functionality is disabled. when not enabled as the porte pin, they are disabled and read as ? 0 ?. the 28-pin devices have only re3 on porte when mclr is disabled.
? 2003 microchip technology inc. preliminary ds39616b-page 51 pic18f2331/2431/4331/4431 ipr2 2331 2431 4331 4431 1--1 -1-1 1--1 -1-1 u--u -u-u pir2 2331 2431 4331 4431 0--0 -0-0 0--0 -0-0 u--u -u-u pie2 2331 2431 4331 4431 0--0 -0-0 0--0 -0-0 u--u -u-u ipr1 2331 2431 4331 4431 1111 1111 1111 1111 uuuu uuuu 2331 2431 4331 4431 -111 1111 -111 1111 -uuu uuuu pir1 2331 2431 4331 4431 -000 0000 -000 0000 -uuu uuuu (1) 2331 2431 4331 4431 -000 0000 -000 0000 -uuu uuuu (1) pie1 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu 2331 2431 4331 4431 -000 0000 -000 0000 -uuu uuuu osctune 2331 2431 4331 4431 --00 0000 --00 0000 --uu uuuu adcon3 2331 2431 4331 4431 00-0 0000 00-0 0000 uu-u uuuu adchs 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu trise (6) 2331 2431 4331 4431 ---- -111 ---- -111 ---- -uuu trisd 2331 2431 4331 4431 1111 1111 1111 1111 uuuu uuuu trisc 2331 2431 4331 4431 1111 1111 1111 1111 uuuu uuuu trisb 2331 2431 4331 4431 1111 1111 1111 1111 uuuu uuuu trisa (5) 2331 2431 4331 4431 1111 1111 (5) 1111 1111 (5) uuuu uuuu (5) pr5h 2331 2431 4331 4431 1111 1111 1111 1111 uuuu uuuu pr5l 2331 2431 4331 4431 1111 1111 1111 1111 uuuu uuuu late (6) 2331 2431 4331 4431 ---- -xxx ---- -uuu ---- -uuu latd 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu latc 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu latb 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu lata (5) 2331 2431 4331 4431 xxxx xxxx (5) uuuu uuuu (5) uuuu uuuu (5) tmr5h 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu tmr5l 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu porte (6) 2331 2431 4331 4431 ---- xxxx ---- xxxx ---- uuuu portd 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu portc 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu portb 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu porta (5) 2331 2431 4331 4431 xx0x 0000 (5) uu0u 0000 (5) uuuu uuuu (5) table 4-3: initialization conditions for all registers (continued) register applicable devices power-on reset, brown-out reset mclr resets wdt reset reset instruction stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hardware stack. 4: see table 4-2 for reset value for specific condition. 5: bits 6 and 7 of porta, lata and trisa are enabled, depending on the oscillator mode selected. when not enabled as porta pins, they are disabled and read ? 0 ?. 6: bit 3 of porte and late are enabled if mclr functionality is disabled. when not enabled as the porte pin, they are disabled and read as ? 0 ?. the 28-pin devices have only re3 on porte when mclr is disabled.
pic18f2331/2431/4331/4431 ds39616b-page 52 preliminary ? 2003 microchip technology inc. ptcon0 2331 2431 4331 4431 0000 0000 uuuu uuuu uuuu uuuu ptcon1 2331 2431 4331 4431 00-- ---- 00-- ---- uu-- ---- ptmrl 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu ptmrh 2331 2431 4331 4431 ---- 0000 ---- 0000 ---- uuuu ptperl 2331 2431 4331 4431 1111 1111 1111 1111 uuuu uuuu ptperh 2331 2431 4331 4431 ---- 1111 ---- 1111 ---- uuuu pdc0l 2331 2431 4331 4431 --00 0000 --00 0000 --uu uuuu pdc0h 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu pdc1l 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu pdc1h 2331 2431 4331 4431 --00 0000 --00 0000 --uu uuuu pdc2l 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu pdc2h 2331 2431 4331 4431 --00 0000 --00 0000 --uu uuuu pdc3l 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu pdc3h 2331 2431 4331 4431 --00 0000 --00 0000 --uu uuuu sevtcmpl 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu sevtcmph 2331 2431 4331 4431 ---- 0000 ---- 0000 ---- uuuu pwmcon0 2331 2431 4331 4431 -101 0000 -101 0000 -uuu uuuu pwmcon1 2331 2431 4331 4431 0000 0-00 0000 0-00 uuuu u-uu dtcon 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu fltconfig 2331 2431 4331 4431 -000 0000 -000 0000 -uuu uuuu ovdcond 2331 2431 4331 4431 1111 1111 1111 1111 uuuu uuuu ovdcons 2331 2431 4331 4431 0000 0000 0000 0000 uuuu uuuu cap1bufh/ velrh 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu cap1bufl/ velrl 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu cap2bufh/ poscnth 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu cap2bufl/ poscntl 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu table 4-3: initialization conditi ons for all registers (continued) register applicable devices power-on reset, brown-out reset mclr resets wdt reset reset instruction stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hardware stack. 4: see table 4-2 for reset value for specific condition. 5: bits 6 and 7 of porta, lata and trisa are enabled, depending on the oscillator mode selected. when not enabled as porta pins, they are disabled and read ? 0 ?. 6: bit 3 of porte and late are enabled if mclr functionality is disabled. when not enabled as the porte pin, they are disabled and read as ? 0 ?. the 28-pin devices have only re3 on porte when mclr is disabled.
? 2003 microchip technology inc. preliminary ds39616b-page 53 pic18f2331/2431/4331/4431 cap3bufh/ maxcnth 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu cap3bufl/ maxcntl 2331 2431 4331 4431 xxxx xxxx uuuu uuuu uuuu uuuu cap1con 2331 2431 4331 4431 -0-- 0000 -0-- 0000 -u-- uuuu cap2con 2331 2431 4331 4431 -0-- 0000 -0-- 0000 -u-- uuuu cap3con 2331 2431 4331 4431 -0-- 0000 -0-- 0000 -u-- uuuu dfltcon 2331 2431 4331 4431 -000 0000 -000 0000 -uuu uuuu table 4-3: initialization conditions for all registers (continued) register applicable devices power-on reset, brown-out reset mclr resets wdt reset reset instruction stack resets wake-up via wdt or interrupt legend: u = unchanged, x = unknown, - = unimplemented bit, read as ? 0 ?, q = value depends on condition. shaded cells indicate conditions do not apply for the designated device. note 1: one or more bits in the intconx or pirx registers will be affected (to cause wake-up). 2: when the wake-up is due to an interrupt and the giel or gieh bit is set, the pc is loaded with the interrupt vector (0008h or 0018h). 3: when the wake-up is due to an interrupt and the giel or gieh bit is set, the tosu, tosh and tosl are updated with the current value of the pc. the stkptr is modified to point to the next location in the hardware stack. 4: see table 4-2 for reset value for specific condition. 5: bits 6 and 7 of porta, lata and trisa are enabled, depending on the oscillator mode selected. when not enabled as porta pins, they are disabled and read ? 0 ?. 6: bit 3 of porte and late are enabled if mclr functionality is disabled. when not enabled as the porte pin, they are disabled and read as ? 0 ?. the 28-pin devices have only re3 on porte when mclr is disabled.
pic18f2331/2431/4331/4431 ds39616b-page 54 preliminary ? 2003 microchip technology inc. figure 4-3: time-out sequence on power-up (mclr tied to v dd , v dd rise < t pwrt ) figure 4-4: time-out sequence on power-up (mclr not tied to v dd ): case 1 figure 4-5: time-out sequence on power-up (mclr not tied to v dd ): case 2 t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset v dd mclr internal por pwrt time-out ost time-out internal reset t pwrt t ost
? 2003 microchip technology inc. preliminary ds39616b-page 55 pic18f2331/2431/4331/4431 figure 4-6: slow rise time (mclr tied to v dd , v dd rise > t pwrt ) figure 4-7: time-out sequence on por w/ pll enabled (mclr tied to v dd ) v dd mclr internal por pwrt time-out ost time-out internal reset 0v 1v 5v t pwrt t ost t pwrt t ost v dd mclr internal por pwrt time-out ost time-out internal reset pll time-out t pll note: t ost = 1024 clock cycles. t pll 2 ms max. first three stages of the pwrt timer.
pic18f2331/2431/4331/4431 ds39616b-page 56 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 57 pic18f2331/2431/4331/4431 5.0 memory organization there are three memory types in enhanced mcu devices. these memory types are:  program memory  data ram  data eeprom data and program memory use separate busses, which allows for concurrent access of these types. additional detailed information for flash program mem- ory and data eeprom is provided in section 6.0 ?flash program memory? and section 7.0 ?data eeprom memory? , respectively. figure 5-1: program memory map and stack for pic18f2331/4331 5.1 program memory organization a 21-bit program counter is capable of addressing the 2-mbyte program memory space. accessing a location between the physically implemented memory and the 2-mbyte address will cause a read of all ? 0 ?s (a nop instruction). the pic18f2331 and pic18f4331 each have 8 kbytes of flash memory and can store up to 4,096 single-word instructions. the PIC18F2431 and pic18f4431 each have 16 kbytes of flash memory and can store up to 8,192 single-word instructions. the reset vector address is at 000000h and the interrupt vector addresses are at 000008h and 000018h. the program memory maps for pic18f2x31 and pic18f4x31 devices are shown in figure 5-1 and figure 5-2, respectively. figure 5-2: program memory map and stack for PIC18F2431/4431 pc<20:0> stack level 1 ? stack level 31 reset vector lsb high priority interrupt vector lsb ? ? user memory space call,rcall,return retfie,retlw 21 000000h 000008h 000018h 1fffffh low priority interrupt vector lsb on-chip flash program memory 002000h 001fffh unused - read ? 0 ?s pc<20:0> stack level 1 ? stack level 31 reset vector lsb high priority interrupt vector lsb ? ? user memory space call,rcall,return retfie,retlw 21 000000h 000008h 000018h 1fffffh low priority interrupt vector lsb on-chip flash program memory 004000h 003fffh unused - read ? 0 ?s
pic18f2331/2431/4331/4431 ds39616b-page 58 preliminary ? 2003 microchip technology inc. 5.2 return address stack the return address stack allows any combination of up to 31 program calls and interrupts to occur. the pc (program counter) is pushed onto the stack when a call or rcall instruction is executed, or an interrupt is acknowledged. the pc value is pulled off the stack on a return, retlw or a retfie instruction. pclatu and pclath are not affected by any of the return or call instructions. the stack operates as a 31-word by 21-bit ram and a 5-bit stack pointer, with the stack pointer initialized to 00000b after all resets. there is no ram associated with stack pointer 00000b. this is only a reset value. during a call type instruction, causing a push onto the stack, the stack pointer is first incremented and the ram location pointed to by the stack pointer is written with the contents of the pc (already pointing to the instruction following the call). during a return type instruction, causing a pop from the stack, the contents of the ram location pointed to by the stkptr are transferred to the pc and then the stack pointer is decremented. the stack space is not part of either program or data space. the stack pointer is readable and writable, and the address on the top of the stack is readable and writ- able through the top-of-stack special file registers. data can also be pushed to, or popped from, the stack using the top-of-stack sfrs. status bits indicate if the stack is full, has overflowed or underflowed. 5.2.1 top-of-stack access the top of the stack is readable and writable. three register locations, tosu, tosh and tosl hold the contents of the stack location pointed to by the stkptr register (figure 5-3). this allows users to implement a software stack if necessary. after a call, rcall or interrupt, the software can read the pushed value by reading the tosu, tosh and tosl registers. these values can be placed on a user-defined software stack. at return time, the software can replace the tosu, tosh and tosl and do a return. the user must disable the global interrupt enable bits while accessing the stack to prevent inadvertent stack corruption. 5.2.2 return stack pointer (stkptr) the stkptr register (register 5-1) contains the stack pointer value, the stkful (stack full) status bit, and the stkunf (stack underflow) status bits. the value of the stack pointer can be 0 through 31. the stack pointer increments before values are pushed onto the stack and decrements after values are popped off the stack. at reset, the stack pointer value will be zero. the user may read and write the stack pointer value. this feature can be used by a real-time operating system for return stack maintenance. after the pc is pushed onto the stack 31 times (without popping any values off the stack), the stkful bit is set. the stkful bit is cleared by software or by a por. the action that takes place when the stack becomes full depends on the state of the stvren (stack over- flow reset enable) configuration bit. (refer to section 22.1 ?configuration bits? for a description of the device configuration bits.) if stvren is set (default), the 31st push will push the (pc + 2) value onto the stack, set the stkful bit, and reset the device. the stkful bit will remain set and the stack pointer will be set to zero. if stvren is cleared, the stkful bit will be set on the 31st push and the stack pointer will increment to 31. any additional pushes will not overwrite the 31st push, and stkptr will remain at 31. when the stack has been popped enough times to unload the stack, the next pop will return a value of zero to the pc and set the stkunf bit, while the stack pointer remains at zero. the stkunf bit will remain set until cleared by software or a por occurs. figure 5-3: return address stack and associated registers note: returning a value of zero to the pc on an underflow has the effect of vectoring the program to the reset vector, where the stack conditions can be verified and appropriate actions can be taken. this is not the same as a reset, as the contents of the sfrs are not affected. 00011 001a34h 11111 11110 11101 00010 00001 00000 00010 return address stack top-of-stack 000d58h tosl tosh tosu 34h 1ah 00h stkptr<4:0>
? 2003 microchip technology inc. preliminary ds39616b-page 59 pic18f2331/2431/4331/4431 register 5-1: stkptr register 5.2.3 push and pop instructions since the top-of-stack (tos) is readable and writable, the ability to push values onto the stack and pull values off the stack without disturbing normal program execu- tion is a desirable option. to push the current pc value onto the stack, a push instruction can be executed. this will increment the stack pointer and load the cur- rent pc value onto the stack. tosu, tosh and tosl can then be modified to place data or a return address on the stack. the ability to pull the tos value off of the stack and replace it with the value that was previously pushed onto the stack, without disturbing normal execution, is achieved by using the pop instruction. the pop instruc- tion discards the current tos by decrementing the stack pointer. the previous value pushed onto the stack then becomes the tos value. 5.2.4 stack full/underflow resets these resets are enabled by programming the stvren bit in configuration register 4l. when the stvren bit is cleared, a full or underflow condition will set the appropriate stkful or stkunf bit, but not cause a device reset. when the stvren bit is set, a full or underflow will set the appropriate stkful or stkunf bit and then cause a device reset. the stkful or stkunf bits are cleared by the user software or a por reset. r/c-0 r/c-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 stkful stkunf ? sp4 sp3 sp2 sp1 sp0 bit 7 bit 0 bit 7 (1) stkful: stack full flag bit 1 = stack became full or overflowed 0 = stack has not become full or overflowed bit 6 (1) stkunf: stack underflow flag bit 1 = stack underflow occurred 0 = stack underflow did not occur bit 5 unimplemented: read as ? 0 ? bit 4-0 sp4:sp0: stack pointer location bits note 1: bit 7 and bit 6 are cleared by user software or by a por. legend: r = readable bit w = writable bit u = unimplemented c = clearable only bit - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 60 preliminary ? 2003 microchip technology inc. 5.3 fast register stack a ?fast return? option is available for interrupts. a fast register stack is provided for the status, wreg and bsr registers and are only one in depth. the stack is not readable or writable and is loaded with the current value of the corresponding register when the processor vectors for an interrupt. the values in the registers are then loaded back into the working registers, if the retfie, fast instruction is used to return from the interrupt. all interrupt sources will push values into the stack reg- isters. if both low and high priority interrupts are enabled, the stack registers cannot be used reliably to return from low priority interrupts. if a high priority inter- rupt occurs while servicing a low priority interrupt, the stack register values stored by the low priority interrupt will be overwritten. users must save the key registers in software during a low priority interrupt. if interrupt priority is not used, all interrupts may use the fast register stack for returns from interrupt. if no interrupts are used, the fast register stack can be used to restore the status, wreg and bsr registers at the end of a subroutine call. to use the fast register stack for a subroutine call, a call label, fast instruction must be executed to save the status, wreg and bsr registers to the fast register stack. a return, fast instruction is then executed to restore these registers from the fast register stack. example 5-1 shows a source code example that uses the fast register stack during a subroutine call and return. example 5-1: fast register stack code example 5.4 pcl, pclath and pclatu the program counter (pc) specifies the address of the instruction to fetch for execution. the pc is 21-bits wide. the low byte, known as the pcl register, is both readable and writable. the high byte, or pch register, contains the pc<15:8> bits and is not directly readable or writable. updates to the pch register may be per- formed through the pclath register. the upper byte is called pcu. this register contains the pc<20:16> bits and is not directly readable or writable. updates to the pcu register may be performed through the pclatu register. the contents of pclath and pclatu will be trans- ferred to the program counter by any operation that writes pcl. similarly, the upper two bytes of the pro- gram counter will be transferred to pclath and pclatu by an operation that reads pcl. this is useful for computed offsets to the pc (see section 5.8.1 ?computed goto ? ). the pc addresses bytes in the program memory. to prevent the pc from becoming misaligned with word instructions, the lsb of pcl is fixed to a value of ? 0 ?. the pc increments by 2 to address sequential instructions in the program memory. the call, rcall, goto and program branch instructions write to the program counter directly. for these instructions, the contents of pclath and pclatu are not transferred to the program counter. call sub1, fast ;status, wreg, bsr ;saved in fast register ;stack ? ? sub1 ? ? return fast ;restore values saved ;in fast register stack
? 2003 microchip technology inc. preliminary ds39616b-page 61 pic18f2331/2431/4331/4431 5.5 clocking scheme/instruction cycle the clock input (from osc1) is internally divided by four to generate four non-overlapping quadrature clocks, namely q1, q2, q3 and q4. internally, the pro- gram counter (pc) is incremented every q1, the instruction is fetched from the program memory and latched into the instruction register in q4. the instruc- tion is decoded and executed during the following q1 through q4. the clocks and instruction execution flow are shown in figure 5-4. 5.6 instruction flow/pipelining an ?instruction cycle? consists of four q cycles (q1, q2, q3 and q4). the instruction fetch and execute are pipelined such that fetch takes one instruction cycle, while decode and execute takes another instruction cycle. however, due to the pipelining, each instruction effectively executes in one cycle. if an instruction causes the program counter to change (e.g., goto ), then two cycles are required to complete the instruction (example 5-2). a fetch cycle begins with the program counter (pc) incrementing in q1. in the execution cycle, the fetched instruction is latched into the ?instruction register? (ir) in cycle q1. this instruction is then decoded and executed during the q2, q3, and q4 cycles. data memory is read during q2 (operand read) and written during q4 (destination write). figure 5-4: clock/ instruction cycle example 5-2: instruction pipeline flow q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 q1 q2 q3 q4 pc osc2/clko (rc mode) pc pc+2 pc+4 fetch inst (pc) execute inst (pc-2) fetch inst (pc+2) execute inst (pc) fetch inst (pc+4) execute inst (pc+2) internal phase clock all instructions are single cycle, except for any program branches. these take two cycles since the fetch instruction is ?flushed? from the pipeline, while the new instruction is being fetched and then executed. t cy 0t cy 1t cy 2t cy 3t cy 4t cy 5 1. movlw 55h fetch 1 execute 1 2. movwf portb fetch 2 execute 2 3. bra sub_1 fetch 3 execute 3 4. bsf porta, bit3 (forced nop) fetch 4 flush ( nop ) 5. instruction @ address sub_1 fetch sub_1 execute sub_1
pic18f2331/2431/4331/4431 ds39616b-page 62 preliminary ? 2003 microchip technology inc. 5.7 instructions in program memory the program memory is addressed in bytes. instruc- tions are stored as two bytes or four bytes in program memory. the least significant byte of an instruction word is always stored in a program memory location with an even address (lsb = 0 ). figure 5-5 shows an example of how instruction words are stored in the pro- gram memory. to maintain alignment with instruction boundaries, the pc increments in steps of 2 and the lsb will always read ? 0 ? (see section 5.4 ?pcl, pclath and pclatu? ). the call and goto instructions have the absolute pro- gram memory address embedded into the instruction. since instructions are always stored on word bound- aries, the data contained in the instruction is a word address. the word address is written to pc<20:1>, which accesses the desired byte address in program memory. instruction #2 in figure 5-5 shows how the instruction ? goto 000006h ? is encoded in the program memory. program branch instructions, which encode a relative address offset, operate in the same manner. the offset value stored in a branch instruction repre- sents the number of single-word instructions that the pc will be offset by. section 23.0 ?instruction set summary? provides further details of the instruction set. figure 5-5: instructions in program memory 5.7.1 two-word instructions pic18f2331/2431/4331/4431 devices have four two- word instructions: movff, call, goto and lfsr . the second word of these instructions has the 4 msbs set to ? 1 ?s and is decoded as a nop instruction. the lower 12 bits of the second word contain data to be used by the instruction. if the first word of the instruction is executed, the data in the second word is accessed. if the second word of the instruction is executed by itself (first word was skipped), it will execute as a nop . this action is necessary when the two-word instruction is preceded by a conditional instruction that results in a skip operation. a program example that demonstrates this concept is shown in example 5-3. refer to section 23.0 ?instruction set summary? for further details of the instruction set. example 5-3: two-word instructions word address lsb = 1 lsb = 0 program memory byte locations 000000h 000002h 000004h 000006h instruction 1: movlw 055h 0fh 55h 000008h instruction 2: goto 000006h efh 03h 00000ah f0h 00h 00000ch instruction 3: movff 123h, 456h c1h 23h 00000eh f4h 56h 000010h 000012h 000014h case 1: object code source code 0110 0110 0000 0000 tstfsz reg1 ; is ram location 0? 1100 0001 0010 0011 movff reg1, reg2 ; no, skip this word 1111 0100 0101 0110 ; execute this word as a nop 0010 0100 0000 0000 addwf reg3 ; continue code case 2: object code source code 0110 0110 0000 0000 tstfsz reg1 ; is ram location 0? 1100 0001 0010 0011 movff reg1, reg2 ; yes, execute this word 1111 0100 0101 0110 ; 2nd word of instruction 0010 0100 0000 0000 addwf reg3 ; continue code
? 2003 microchip technology inc. preliminary ds39616b-page 63 pic18f2331/2431/4331/4431 5.8 look-up tables look-up tables are implemented two ways:  computed goto  table reads 5.8.1 computed goto a computed goto is accomplished by adding an offset to the program counter. an example is shown in example 5-4. a look-up table can be formed with an addwf pcl instruction and a group of retlw 0xnn instructions. wreg is loaded with an offset into the table before executing a call to that table. the first instruction of the called routine is the addwf pcl instruction. the next instruction executed will be one of the retlw 0xnn instructions, which returns the value 0xnn to the calling function. the offset value (in wreg) specifies the number of bytes that the program counter should advance, and should be multiples of 2 (lsb = 0 ). in this method, only one data byte may be stored in each instruction location and room on the return address stack is required. example 5-4: computed goto using an offset value 5.8.2 table reads/table writes a better method of storing data in program memory allows two bytes of data to be stored in each instruction location. look-up table data may be stored two bytes per pro- gram word by using table reads and writes. the table pointer (tblptr) specifies the byte address and the table latch (tablat) contains the data that is read from, or written to program memory. data is transferred to/from program memory, one byte at a time. the table read/table write operation is discussed further in section 6.1 ?table reads and table writes? . 5.9 data memory organization the data memory is implemented as static ram. each register in the data memory has a 12-bit address, allowing up to 4096 bytes of data memory. figure 5-6 shows the data memory organization for the pic18f2331/2431/4331/4431 devices. the data memory map is divided into as many as 16 banks that contain 256 bytes each. the lower 4 bits of the bank select register (bsr<3:0>) select which bank will be accessed. the upper 4 bits for the bsr are not implemented. the data memory contains special function registers (sfr) and general purpose registers (gpr). the sfrs are used for control and status of the controller and peripheral functions, while gprs are used for data storage and scratch pad operations in the user?s application. the sfrs start at the last location of bank 15 (fffh) and extend to f60h. any remaining space beyond the sfrs in the bank may be implemented as gprs. gprs start at the first location of bank 0 and grow upwards. any read of an unimplemented location will read as ? 0 ?s. the entire data memory may be accessed directly or indirectly. direct addressing may require the use of the bsr register. indirect addressing requires the use of a file select register (fsrn) and a corresponding indirect file operand (indfn). each fsr holds a 12- bit address value that can be used to access any location in the data memory map without banking. see section 5.12 ?indirect addressing, indf and fsr registers? for indirect addressing details. the instruction set and architecture allow operations across all banks. this may be accomplished by indirect addressing or by the use of the movff instruction. the movff instruction is a two-word/two-cycle instruction that moves a value from one register to another. to ensure that commonly used registers (sfrs and select gprs) can be accessed in a single cycle, regardless of the current bsr values, an access bank is implemented. a segment of bank 0 and a segment of bank 15 comprise the access ram. section 5.10 ?access bank? provides a detailed description of the access ram. 5.9.1 general purpose register file enhanced mcu devices may have banked memory in the gpr area. gprs are not initialized by a power-on reset and are unchanged on all other resets. data ram is available for use as gpr registers by all instructions. the second half of bank 15 (f60h to fffh) contains sfrs. all other banks of data memory contain gprs, starting with bank 0. movfwoffset calltable org 0xnn00 tableaddwfpcl retlw0xnn retlw0xnn retlw0xnn . . .
pic18f2331/2431/4331/4431 ds39616b-page 64 preliminary ? 2003 microchip technology inc. figure 5-6: data memory map for pic18f2331/2431/4331/4431 devices bank 0 bank 1 bank 14 bank 15 data memory map bsr<3:0> = 0000 = 0001 = 1111 060h 05fh f60h fffh 00h 5fh 60h ffh access bank when a = 0 : the bsr is ignored and the access bank is used. the first 96 bytes are general purpose ram (from bank 0). the second 160 bytes are special function registers (from bank 15). when a = 1 : the bsr specifies the bank used by the instruction. f5fh f00h effh 1ffh 100h 0ffh 000h access ram ffh 00h ffh 00h ffh 00h gpr gpr sfr unused access ram high access ram low bank 3 to 200h unused read ?00h? = 1110 = 0011 (sfrs) = 0010 gpr ffh 00h 00h bank 2 300h 2ffh
? 2003 microchip technology inc. preliminary ds39616b-page 65 pic18f2331/2431/4331/4431 5.9.2 special function registers the special function registers (sfrs) are registers used by the cpu and peripheral modules for control- ling the desired operation of the device. these regis- ters are implemented as static ram. a list of these registers is given in table 5-1 and table 5-2. the sfrs can be classified into two sets; those asso- ciated with the ?core? function and those related to the peripheral functions. those registers related to the ?core? are described in this section, while those related to the operation of the peripheral features are described in the section of that peripheral feature. the sfrs are typically distributed among the peripherals whose functions they control. the unused sfr locations will be unimplemented and read as ? 0 ?s. table 5-1: special function register map for pic18f2331/2431/4331/4431 devices address name address name address name address name address name fffh tosu fdfh indf2 fbfh ccpr1h f9fh ipr1 f7fh ptcon0 ffeh tosh fdeh postinc2 fbeh ccpr1l f9eh pir1 f7eh ptcon1 ffdh tosl fddh postdec2 fbdh ccp1con f9dh pie1 f7dh ptmrl ffch stkptr fdch preinc2 fbch ccpr2h f9ch ? f7ch ptmrh ffbh pclatu fdbh plusw2 fbbh ccpr2l f9bh osctune f7bh ptperl ffah pclath fdah fsr2h fbah ccp2con f9ah adcon3 f7ah ptperh ff9h pcl fd9h fsr2l fb9h ansel1 f99h adchs f79h pdc0l ff8h tblptru fd8h status fb8h ansel0 f98h ? f78h pdc0h ff7h tblptrh fd7h tmr0h fb7h t5con f97h ? f77h pdc1l ff6h tblptrl fd6h tmr0l fb6h qeicon f96h trise f76h pdc1h ff5h tablat fd5h t0con fb5h ? f95h trisd f75h pdc2l ff4h prodh fd4h ? fb4h ? f94h trisc f74h pdc2h ff3h prodl fd3h osccon fb3h ? f93h trisb f73h pdc3l ff2h intcon fd2h lvdcon fb2h ? f92h trisa f72h pdc3h ff1h intcon2 fd1h wdtcon fb1h ? f91h pr5h f71h sevtcmpl ff0h intcon3 fd0h rcon fb0h spbrgh f90h pr5l f70h sevtcmph fefh indf0 fcfh tmr1h fafh spbrg f8fh ? f6fh pwmcon0 feeh postinc0 fceh tmr1l faeh rcreg f8eh ? f6eh pwmcon1 fedh postdec0 fcdh t1con fadh txreg f8dh late f6dh dtcon fech preinc0 fcch tmr2 fach txsta f8ch latd f6ch fltconfig febh plusw0 fcbh pr2 fabh rcsta f8bh latc f6bh ovdcond feah fsr0h fcah t2con faah baudctl f8ah latb f6ah ovdcons fe9h fsr0l fc9h sspbuf fa9h eeadr f89h lata f69h cap1bufh fe8h wreg fc8h sspadd fa8h eedata f88h tmr5h f68h cap1bufl fe7h indf1 fc7h sspstat fa7h eecon2 f87h tmr5l f67h cap2bufh fe6h postinc1 fc6h sspcon fa6h eecon1 f86h ? f66h cap2bufl fe5h postdec1 fc5h ? fa5h ipr3 f85h ? f65h cap3bufh fe4h preinc1 fc4h adresh fa4h pir3 f84h porte f64h cap3bufl fe3h plusw1 fc3h adresl fa3h pie3 f83h portd f63h cap1con fe2h fsr1h fc2h adcon0 fa2h ipr2 f82h portc f62h cap2con fe1h fsr1l fc1h adcon1 fa1h pir2 f81h portb f61h cap3con fe0h bsr fc0h adcon2 fa0h pie2 f80h porta f60h dfltcon
pic18f2331/2431/4331/4431 ds39616b-page 66 preliminary ? 2003 microchip technology inc. table 5-2: register file summary (pic18f2331/2431/4331/4431) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: tosu ? ? ? top-of-stack upper byte (tos<20:16>) ---0 0000 48, 58 tosh top-of-stack high byte (tos<15:8>) 0000 0000 48, 58 tosl top-of-stack low byte (tos<7:0>) 0000 0000 48, 58 stkptr stkful stkunf ? return stack pointer 00-0 0000 48, 59 pclatu ? ?bit 21 (3) holding register for pc<20:16> ---0 0000 48, 60 pclath holding register for pc<15:8> 0000 0000 48, 60 pcl pc low byte (pc<7:0>) 0000 0000 48, 60 tblptru ? ?bit 21 (3) program memory table pointer upper byte (tblptr<20:16>) --00 0000 48, 78 tblptrh program memory table pointer high byte (tblptr<15:8>) 0000 0000 48, 78 tblptrl program memory table pointer low byte (tblptr<7:0>) 0000 0000 48, 78 tablat program memory table latch 0000 0000 48, 78 prodh product register high byte xxxx xxxx 48, 89 prodl product register low byte xxxx xxxx 48, 89 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0f rbif 0000 000x 48, 93 intcon2 rbpu intedg0 intedg1 intedg2 ?tmr0ip ?rbip 1111 -1-1 48, 94 intcon3 int2p int1p ?int2ieint1ie ? int2if int1if 11-0 0-00 48, 95 indf0 uses contents of fsr0 to address data memory ? value of fsr0 not changed (not a physical register) n/a 48, 71 postinc0 uses contents of fsr0 to address data memory ? value of fsr0 post-incremented (not a physical register) n/a 48, 71 postdec0 uses contents of fsr0 to address data memory ? value of fsr0 post-decremented (not a physical register) n/a 48, 71 preinc0 uses contents of fsr0 to address data memory ? value of fsr0 pre-incremented (not a physical register) n/a 48, 71 plusw0 uses contents of fsr0 to address data memory ? value of fsr0 offset by w (not a physical register) n/a 48, 71 fsr0h ? ? ? ? indirect data memory address pointer 0 high ---- 0000 48, 71 fsr0l indirect data memory address pointer 0 low byte xxxx xxxx 48, 71 wreg working register xxxx xxxx 48 indf1 uses contents of fsr1 to address data memory ? value of fsr1 not changed (not a physical register) n/a 48, 71 postinc1 uses contents of fsr1 to address data memory ? value of fsr1 post-incremented (not a physical register) n/a 48, 71 postdec1 uses contents of fsr1 to address data memory ? value of fsr1 post-decremented (not a physical register) n/a 48, 71 preinc1 uses contents of fsr1 to address data memory ? value of fsr1 pre-incremented (not a physical register) n/a 48, 71 plusw1 uses contents of fsr1 to address data memory ? value of fsr1 offset by w (not a physical register) n/a 48, 71 fsr1h ? ? ? ? indirect data memory address pointer 1 high ---- 0000 49, 71 fsr1l indirect data memory address pointer 1 low byte xxxx xxxx 49, 71 bsr ? ? ? ? bank select register ---- 0000 49, 70 indf2 uses contents of fsr2 to address data memory ? value of fsr2 not changed (not a physical register) n/a 49, 71 postinc2 uses contents of fsr2 to address data memory ? value of fsr2 post-incremented (not a physical register) n/a 49, 71 postdec2 uses contents of fsr2 to address data memory ? value of fsr2 post-decremented (not a physical register) n/a 49, 71 preinc2 uses contents of fsr2 to address data memory ? value of fsr2 pre-incremented (not a physical register) n/a 49, 71 plusw2 uses contents of fsr2 to address data memory ? value of fsr2 offset by w (not a physical register) n/a 49, 71 fsr2h ? ? ? ? indirect data memory address pointer 2 high ---- 0000 49, 71 fsr2l indirect data memory address pointer 2 low byte xxxx xxxx 49, 71 status ? ? ?novzdcc ---x xxxx 49, 73 tmr0h timer0 register high byte 0000 0000 49, 135 tmr0l timer0 register low byte xxxx xxxx 49, 135 t0con tmr0on t016bit ? ? t0ps3 t0ps2 t0ps1 t0ps0 11-- 1111 49, 133 legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: ra6 and associated bits are configured as port pins in rcio, ecio and intio2 (with port function on ra6) oscillator mode only, and read ? 0 ? in all other oscillator modes. 2: ra7 and associated bits are configured as port pins in intio2 oscillator mode only and read ? 0 ? in all other modes. 3: bit 21 of the pc is only available in test mode and serial programming modes. 4: if pbaden = 0 , portb<4:0> are configured as digital input and read unknown, and if pbaden = 1 , portb<4:0> are configured as analog input and read ? 0 ? following a reset. 5: these registers and/or bits are not implemented on the pic18f2x31 devices, and read as ? 0 ?. 6: the re3 port bit is only available when mclre fuse (config3h<7>) is programmed to ? 0 ?. otherwise, re3 reads ? 0 ?. this bit is read-only.
? 2003 microchip technology inc. preliminary ds39616b-page 67 pic18f2331/2431/4331/4431 osccon idlen ircf2 ircf1 ircf0 osts iofs scs1 scs0 0000 q000 28, 49 lvdcon ? ? ivrst lvden lvdl3 lvdl2 lvdl1 lvdl0 --00 0101 49, 263 wdtcon wdtw ? ? ? ? ? ? swdten 0000 0000 49, 279 rcon ipen ? ?ri to pd por bor 0--1 11qq 47, 74, 105 tmr1h timer1 register high byte xxxx xxxx 49, 141 tmr1l timer1 register low byte xxxx xxxx 49, 141 t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 0000 0000 49, 137 tmr2 timer2 register 0000 0000 49, 143 pr2 timer2 period register 1111 1111 49, 143 t2con ? toutps3 toutps2 toutps1 toutps0 tmr2on t2ckps1 t2ckps0 -000 0000 49, 143 sspbuf ssp receive buffer/transmit register xxxx xxxx 49, 220 sspadd ssp address register in i 2 c slave mode. ssp baud rate reload register in i 2 c master mode. 0000 0000 49, 220 sspstat smp cke d/a psr/w ua bf 0000 0000 49, 212 sspcon wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 0000 0000 49, 213 adresh a/d result register high byte xxxx xxxx 50, 259 adresl a/d result register low byte xxxx xxxx 50, 259 adcon0 ? ? aconv acsch acmod1 acmod0 go/done adon --00 0000 50, 244 adcon1 vcfg1 vcfg0 ? fifoen bfemt ffovfl adpnt1 adpnt0 00-0 1000 50, 245 adcon2 adfm acqt3 acqt2 acqt1 acqt0 adcs2 adcs1 adcs0 0000 0000 50, 246 adcon3 adrs1 adrs0 ? ssrc4 ssrc3 ssrc2 ssrc1 ssrc0 00-0 0000 51. 247 adcsh gdsel1 gdsel0 gbsel1 gbsel0 gcsel1 gcsel0 gasel1 gasel0 0000 0000 51, 248 ccpr1h capture/compare/pwm register1 high byte xxxx xxxx 50, 152 ccpr1l capture/compare/pwm register1 low byte xxxx xxxx 50, 152 ccp1con ? ? dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 0000 0000 50, 155, 149 ccpr2h capture/compare/pwm register2 high byte xxxx xxxx 50, 152 ccpr2l capture/compare/pwm register2 low byte xxxx xxxx 50, 152 ccp2con ? ? dc2b1 dc2b0 ccp2m3 ccp2m2 ccp2m1 ccp2m0 --00 0000 50, 155 ansel1 ? ? ? ? ? ? ?ans8 ---- ---1 50, 249 ansel0 ans7 (6) ans6 (6) ans5 (6) ans4 ans3 ans2 ans1 ans0 1111 1111 50, 249 t5con t5sen resen (5) t5mod t5ps1 t5ps0 t5sync tmr5cs tmr5on 0100 0000 50, 145 qeicon velm error up/down qeim2 qeim1 qeim0 pdec1 pdec0 0000 0000 50, 171 spbrgh baud rate generator register, high byte 0000 0000 50, 225 spbrg usart baud rate generator 0000 0000 50, 225 rcreg usart receive register 0000 0000 50, 233, 232 txreg usart transmit register 0000 0000 50, 230, 232 txsta csrc tx9 txen sync ? brgh trmt tx9d 0000 -010 50, 222 rcsta spen rx9 sren cren aden ferr oerr rx9d 0000 000x 50, 223 baudctl ? rcidl ? sckp brg16 ? wue abden -1-1 0-00 50, 224 table 5-2: register file summary (pic18f2331/2431/4331/4431) (continued) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: ra6 and associated bits are configured as port pins in rcio, ecio and intio2 (with port function on ra6) oscillator mode only, and read ? 0 ? in all other oscillator modes. 2: ra7 and associated bits are configured as port pins in intio2 oscillator mode only and read ? 0 ? in all other modes. 3: bit 21 of the pc is only available in test mode and serial programming modes. 4: if pbaden = 0 , portb<4:0> are configured as digital input and read unknown, and if pbaden = 1 , portb<4:0> are configured as analog input and read ? 0 ? following a reset. 5: these registers and/or bits are not implemented on the pic18f2x31 devices, and read as ? 0 ?. 6: the re3 port bit is only available when mclre fuse (config3h<7>) is programmed to ? 0 ?. otherwise, re3 reads ? 0 ?. this bit is read-only.
pic18f2331/2431/4331/4431 ds39616b-page 68 preliminary ? 2003 microchip technology inc. eeadr eeprom address register 0000 0000 50, 85 eedata eeprom data register 0000 0000 50, 88 eecon2 eeprom control register2 (not a physical register) 0000 0000 50, 76, 85 eecon1 eepgd cfgs ? free wrerr wren wr rd xx-0 x000 50, 77, 86 ipr3 ? ? ? ptip ic3drip ic2qeip ic1ip tmr5ip ---1 1111 50 pir3 ? ? ? ptif ic3drif ic2qeif ic1if tmr5if ---0 0000 50 pie3 ? ? ? ptie ic3drie ic2qeie ic1ie tmr5ie ---0 0000 50 ipr2 osfip ? ?eeip ?lvdip ? ccp2ip 1--1 -1-1 51, 103 pir2 osfif ? ? eeif ?lvdif ? ccp2if 0--0 -0-0 51, 97 pie2 osfie ? ?eeie ?lvdie ? ccp2ie 0--0 -0-0 51, 100 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip -111 1111 51, 102 pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if -000 0000 51, 96 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie -000 0000 51, 99 osctune ? ? tun5 tun4 tun3 tun2 tun1 tun0 --00 0000 25, 51 adcon3 adrs1 adrs0 ? ssrc4 ssrc3 ssrc2 ssrc1 ssrc0 00-0 0000 50 adchs gdsel1 gdsel0 gbsel1 gbsel0 gcsel1 gcsel0 gasel1 gasel0 0000 0000 50 trise (5) ? ? ? ? ? data direction bits for porte (5) ---- -111 51, 131 trisd (5) data direction control register for portd 1111 1111 51, 128 trisc data direction control register for portc 1111 1111 51, 123 trisb data direction control register for portb 1111 1111 51, 117 trisa trisa7 (2) trisa6 (1) data direction control register for porta 1111 1111 51, 111 pr5h timer5 period register high byte 1111 1111 50 pr5l timer5 period register low byte 1111 1111 50 late (5) ? ? ? ? ? read/write porte data latch ---- -xxx 51, 132 latd (5) read/write portd data latch xxxx xxxx 51, 128 latc read/write portc data latch xxxx xxxx 51, 123 latb read/write portb data latch xxxx xxxx 51, 117 lata lata<7> (2) lata<6> (1) read/write porta data latch xxxx xxxx 51, 111 tmr5h timer5 timer register high byte xxxx xxxx 146 tmr5l timer5 timer register low byte xxxx xxxx 146 porte ? ? ? ?re3 (6) read porte pins, write porte data latch (5) ---- xxxx 51, 132 portd read portd pins, write portd data latch xxxx xxxx 51, 128 portc read portc pins, write portc data latch xxxx xxxx 51, 123 portb read portb pins, write portb data latch (4) xxxx xxxx 51, 117 porta ra7 (2) ra6 (1) read porta pins, write porta data latch xx0x 0000 51, 111 ptcon0 ptops3 ptops2 ptops1 ptops0 ptckps1 ptckps0 ptmod1 ptmod0 0000 0000 52, 186 ptcon1 pten ptdir ? ? ? ? ? ? 00-- ---- 52, 186 ptmrl pwm time base register (lower 8 bits). 0000 0000 184 ptmrh unused pwm time base register (upper 4 bits) ---- 0000 184 table 5-2: register file summary (pic18f2331/2431/4331/4431) (continued) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: ra6 and associated bits are configured as port pins in rcio, ecio and intio2 (with port function on ra6) oscillator mode only, and read ? 0 ? in all other oscillator modes. 2: ra7 and associated bits are configured as port pins in intio2 oscillator mode only and read ? 0 ? in all other modes. 3: bit 21 of the pc is only available in test mode and serial programming modes. 4: if pbaden = 0 , portb<4:0> are configured as digital input and read unknown, and if pbaden = 1 , portb<4:0> are configured as analog input and read ? 0 ? following a reset. 5: these registers and/or bits are not implemented on the pic18f2x31 devices, and read as ? 0 ?. 6: the re3 port bit is only available when mclre fuse (config3h<7>) is programmed to ? 0 ?. otherwise, re3 reads ? 0 ?. this bit is read-only.
? 2003 microchip technology inc. preliminary ds39616b-page 69 pic18f2331/2431/4331/4431 ptperl pwm time base period register (lower 8 bits). 1111 1111 184 ptperh unused pwm time base period register (upper 4 bits) ---- 1111 184 pdc0l pwm duty cycle #0l register (lower 8 bits) --00 0000 184 pdc0h unused pwm duty cycle #0h register (upper 6 bits) 0000 0000 184 pdc1l pwm duty cycle #1l register (lower 8 bits) 0000 0000 184 pdc1h unused pwm duty cycle #1h register (upper 6 bits) --00 0000 184 pdc2l pwm duty cycle #2l register (lower 8 bits) 0000 0000 184 pdc2h unused pwm duty cycle #2h register (upper 6 bits) --00 0000 184 pdc3l pwm duty cycle #3l register (lower 8 bits) 0000 0000 184 pdc3h unused pwm duty cycle #3h register (upper 6 bits) --00 0000 184 sevtcmpl pwm special event compare register (lower 8 bits) 0000 0000 n/a sevtcmph unused pwm special event compare reg (upper 4 bits) ---- 0000 n/a pwmcon0 ? pwmen2 pwmen1 pwmen0 pmod3 pmod2 pmod1 pmod0 -101 0000 52, 187 pwmcon1 sevops3 sevops2 sevops1 sevops0 sevtdir ? udis osync 0000 0-00 52, 188 dtcon dtps1 dtps0 dt5 dt4 dt3 dt2 dt1 dt0 0000 0000 52, 200 fltconfig ? fltbs fltbmod fltben fltcon fltas fltamod fltaen -000 0000 52, 208 ovdcond povd7 povd6 povd5 povd4 povd3 povd2 povd1 povd0 1111 1111 52, 203 ovdcons pout7 pout6 pout5 pout4 pout3 pout2 pout1 pout0 0000 0000 52, 204 cap1bufh/ velrh capture 1 register, high byte/ velocity register, high byte xxxx xxxx 52, cap1bufl/ velrl capture 1 register low byte/ velocity register, low byte xxxx xxxx 52 cap2bufh/ poscnth capture 2 register, high byte/ qei position counter register, high byte xxxx xxxx 52 cap2bufl/ poscntl capture 2 reg., low byte/ qei position counter register, low byte xxxx xxxx 52 cap3bufh/ maxcnth capture 3 reg., high byte/ qei max. count limit register, high byte xxxx xxxx 53 cap3bufl/ maxcntl capture 3 reg., low byte/ qei max. count limit register, low byte xxxx xxxx 53 cap1con ? cap1ren ? ? cap1m3 cap1m2 cap1m1 cap1m0 -0-0 0000 53, 163 cap2con ? cap2ren ? ? cap2m3 cap2m2 cap2m1 cap2m0 -0-0 0000 53, 163 cap3con ? cap3ren ? ? cap3m3 cap3m2 cap3m1 cap3m0 -0-0 0000 53, 163 dfltcon ? flt4en flt3en flt2en flt1en fltck2 fltck1 fltck0 -000 0000 53, 178 table 5-2: register file summary (pic18f2331/2431/4331/4431) (continued) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor details on page: legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition note 1: ra6 and associated bits are configured as port pins in rcio, ecio and intio2 (with port function on ra6) oscillator mode only, and read ? 0 ? in all other oscillator modes. 2: ra7 and associated bits are configured as port pins in intio2 oscillator mode only and read ? 0 ? in all other modes. 3: bit 21 of the pc is only available in test mode and serial programming modes. 4: if pbaden = 0 , portb<4:0> are configured as digital input and read unknown, and if pbaden = 1 , portb<4:0> are configured as analog input and read ? 0 ? following a reset. 5: these registers and/or bits are not implemented on the pic18f2x31 devices, and read as ? 0 ?. 6: the re3 port bit is only available when mclre fuse (config3h<7>) is programmed to ? 0 ?. otherwise, re3 reads ? 0 ?. this bit is read-only.
pic18f2331/2431/4331/4431 ds39616b-page 70 preliminary ? 2003 microchip technology inc. 5.10 access bank the access bank is an architectural enhancement which is very useful for c compiler code optimization. the techniques used by the c compiler may also be useful for programs written in assembly. this data memory region can be used for:  intermediate computational values  local variables of subroutines  faster context saving/switching of variables  common variables  faster evaluation/control of sfrs (no banking) the access bank is comprised of the last 128 bytes in bank 15 (sfrs) and the first 128 bytes in bank 0. these two sections will be referred to as access ram high and access ram low, respectively. figure 5-6 indicates the access ram areas. a bit in the instruction word specifies if the operation is to occur in the bank specified by the bsr register or in the access bank. this bit is denoted as the ?a? bit (for access bit). when forced in the access bank (a = 0 ), the last address in access ram low is followed by the first address in access ram high. access ram high maps the special function registers, so these registers can be accessed without any software overhead. this is useful for testing status flags and modifying control bits. 5.11 bank select register (bsr) the need for a large general purpose memory space dictates a ram banking scheme. the data memory is partitioned into as many as sixteen banks. when using direct addressing, the bsr should be configured for the desired bank. bsr<3:0> holds the upper 4 bits of the 12-bit ram address. the bsr<7:4> bits will always read ? 0 ?s, and writes will have no effect (see figure 5-7). a movlb instruction has been provided in the instruction set to assist in selecting banks. if the currently selected bank is not implemented, any read will return all ? 0 ?s and all writes are ignored. the status register bits will be set/cleared as appropriate for the instruction performed. each bank extends up to ffh (256 bytes). all data memory is implemented as static ram. a movff instruction ignores the bsr, since the 12-bit addresses are embedded into the instruction word. section 5.12 ?indirect addressing, indf and fsr registers? provides a description of indirect address- ing, which allows linear addressing of the entire ram space. figure 5-7: direct addressing note 1: for register file map detail, see table 5-1. 2: the access bit of the instruction can be used to force an override of the selected bank (bsr<3:0>) to the registers of the access bank. 3: the movff instruction embeds the entire 12-bit address in the instruction. data memory (1) direct addressing bank select (2) location select (3) bsr<3:0> 7 0 from opcode (3) 00h 01h 0eh 0fh bank 0 bank 1 bank 14 bank 15 1ffh 100h 0ffh 000h effh e00h fffh f00h bsr<7:4> 0000
? 2003 microchip technology inc. preliminary ds39616b-page 71 pic18f2331/2431/4331/4431 5.12 indirect addressing, indf and fsr registers indirect addressing is a mode of addressing data mem- ory, where the data memory address in the instruction is not fixed. an fsr register is used as a pointer to the data memory location that is to be read or written. since this pointer is in ram, the contents can be modified by the program. this can be useful for data tables in the data memory and for software stacks. figure 5-8 shows how the fetched instruction is modified prior to being executed. indirect addressing is possible by using one of the indf registers. any instruction using the indf register actually accesses the register pointed to by the file select register, fsr. reading the indf register itself, indirectly (fsr = 0 ), will read 00h. writing to the indf register indirectly, results in a no operation. the fsr register contains a 12-bit address, which is shown in figure 5-9. the indfn register is not a physical register. address- ing indfn actually addresses the register whose address is contained in the fsrn register (fsrn is a pointer). this is indirect addressing. example 5-5 shows a simple use of indirect addressing to clear the ram in bank 1 (locations 100h-1ffh) in a minimum number of instructions. example 5-5: how to clear ram (bank 1) using indirect addressing there are three indirect addressing registers. to address the entire data memory space (4096 bytes), these registers are 12-bits wide. to store the 12 bits of addressing information, two 8-bit registers are required: 1. fsr0: composed of fsr0h:fsr0l 2. fsr1: composed of fsr1h:fsr1l 3. fsr2: composed of fsr2h:fsr2l in addition, there are registers indf0, indf1 and indf2, which are not physically implemented. reading or writing to these registers activates indirect address- ing, with the value in the corresponding fsr register being the address of the data. if an instruction writes a value to indf0, the value will be written to the address pointed to by fsr0h:fsr0l. a read from indf1 reads the data from the address pointed to by fsr1h:fsr1l. indfn can be used in code anywhere an operand can be used. if indf0, indf1 or indf2 are read indirectly via a fsr, all ? 0 ?s are read (zero bit is set). similarly, if indf0, indf1 or indf2 are written to indirectly, the operation will be equivalent to a nop instruction and the status bits are not affected. 5.12.1 indirect addressing operation each fsr register has an indf register associated with it, plus four additional register addresses. perform- ing an operation using one of these five registers deter- mines how the fsr will be modified during indirect addressing. when data access is performed using one of the five indfn locations, the address selected will configure the fsrn register to:  do nothing to fsrn after an indirect access (no change) ? indfn  auto-decrement fsrn after an indirect access (post-decrement) ? postdecn  auto-increment fsrn after an indirect access (post-increment) ? postincn  auto-increment fsrn before an indirect access (pre-increment) ? preincn  use the value in the wreg register as an offset to fsrn. do not modify the value of the wreg or the fsrn register after an indirect access (no change) ? pluswn when using the auto-increment or auto-decrement fea- tures, the effect on the fsr is not reflected in the status register. for example, if the indirect address causes the fsr to equal ? 0 ?, the z bit will not be set. auto-incrementing or auto-decrementing a fsr affects all 12 bits. that is, when fsrnl overflows from an increment, fsrnh will be incremented automatically. adding these features allows the fsrn to be used as a stack pointer, in addition to its uses for table operations in data memory. each fsr has an address associated with it that per- forms an indexed indirect access. when a data access to this indfn location (pluswn) occurs, the fsrn is configured to add the signed value in the wreg regis- ter and the value in fsr to form the address before an indirect access. the fsr value is not changed. the wreg offset range is -128 to +127. if an fsr register contains a value that points to one of the indfn, an indirect read will read 00h (zero bit is set), while an indirect write will be equivalent to a nop (status bits are not affected). if an indirect addressing write is performed when the target address is an fsrnh or fsrnl register, the data is written to the fsr register, but no pre- or post- increment/decrement is performed. lfsr fsr0, 0x100 ; next clrf postinc0 ; clear indf ; register then ; inc pointer btfss fsr0h, 1 ; all done with ; bank1? goto next ; no, clear next continue ; yes, continue
pic18f2331/2431/4331/4431 ds39616b-page 72 preliminary ? 2003 microchip technology inc. figure 5-8: indirect addressing operation figure 5-9: indirect addressing opcode address file address = access of an indirect addressing register fsr instruction executed instruction fetched ram opcode file 12 12 12 bsr<3:0> 8 4 0h fffh note 1: for register file map detail, see table 5-1. data memory (1) indirect addressing fsrnh:fsrnl 30 0fffh 0000h location select 11 0 07
? 2003 microchip technology inc. preliminary ds39616b-page 73 pic18f2331/2431/4331/4431 5.13 status register the status register, shown in register 5-2, contains the arithmetic status of the alu. the status register can be the operand for any instruction, as with any other reg- ister. if the status register is the destination for an instruction that affects the z, dc, c, ov or n bits, then the write to these five bits is disabled. these bits are set or cleared according to the device logic. therefore, the result of an instruction with the status register as desti- nation may be different than intended. for example, clrf status will clear the upper three bits and set the z bit. this leaves the status register as 000u u1uu (where u = unchanged). it is recommended, therefore, that only bcf, bsf, swapf, movff and movwf instructions are used to alter the status register, because these instructions do not affect the z, c, dc, ov or n bits in the status reg- ister. for other instructions not affecting any status bits, see table 23-2. register 5-2: status register note: the c and dc bits operate as a borrow and digit borrow bit respectively, in sub- traction. u-0 u-0 u-0 r/w-x r/w-x r/w-x r/w-x r/w-x ? ? ?novzdcc bit 7 bit 0 bit 7-5 unimplemented: read as ? 0 ? bit 4 n: negative bit this bit is used for signed arithmetic (2?s complement). it indicates whether the result was negative (alu msb = 1 ). 1 = result was negative 0 = result was positive bit 3 ov: overflow bit this bit is used for signed arithmetic (2?s complement). it indicates an overflow of the 7-bit magnitude, which causes the sign bit (bit7) to change state. 1 = overflow occurred for signed arithmetic (in this arithmetic operation) 0 = no overflow occurred bit 2 z: zero bit 1 = the result of an arithmetic or logic operation is zero 0 = the result of an arithmetic or logic operation is not zero bit 1 dc: digit carry/borrow bit for addwf, addlw, sublw and subwf instructions 1 = a carry-out from the 4th low order bit of the result occurred 0 = no carry-out from the 4th low order bit of the result note: for borrow, the polarity is reversed. a subtraction is executed by adding the 2?s complement of the second operand. for rotate ( rrf, rlf ) instructions, this bit is loaded with either the bit 4 or bit 3 of the source register. bit 0 c: carry/borrow bit for addwf, addlw, sublw and subwf instructions 1 = a carry-out from the most significant bit of the result occurred 0 = no carry-out from the most significant bit of the result occurred note: for borrow, the polarity is reversed. a subtraction is executed by adding the 2?s complement of the second operand. for rotate ( rrf, rlf ) instructions, this bit is loaded with either the high- or low-order bit of the source register. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 74 preliminary ? 2003 microchip technology inc. 5.14 rcon register the reset control (rcon) register contains flag bits that allow differentiation between the sources of a device reset. these flags include the to , pd , por , bor and ri bits. this register is readable and writable. register 5-3: rcon register note 1: if the boren configuration bit is set (brown-out reset enabled), the bor bit is ? 1 ? on a power-on reset. after a brown- out reset has occurred, the bor bit will be cleared and must be set by firmware to indicate the occurrence of the next brown-out reset. 2: it is recommended that the por bit be set after a power-on reset has been detected, so that subsequent power-on resets may be detected. r/w-0 u-0 u-0 r/w-1 r-1 r-1 r/w-0 r/w-0 ipen ? ?ri to pd por bor bit 7 bit 0 bit 7 ipen: interrupt priority enable bit 1 = enable priority levels on interrupts 0 = disable priority levels on interrupts (pic16cxxx compatibility mode) bit 6-5 unimplemented: read as ? 0 ? bit 4 ri : reset instruction flag bit 1 = the reset instruction was not executed (set by firmware only) 0 = the reset instruction was executed causing a device reset (must be set in firmware after a brown-out reset occurs) bit 3 to : watchdog time-out flag bit 1 = set by power-up, clrwdt instruction, or sleep instruction 0 = a wdt time-out occurred bit 2 pd : power-down detection flag bit 1 = set by power-up or by the clrwdt instruction 0 = cleared by execution of the sleep instruction bit 1 por : power-on reset status bit 1 = a power-on reset has not occurred (set by firmware only) 0 = a power-on reset occurred (must be set in firmware after a power-on reset occurs) bit 0 bor : brown-out reset status bit 1 = a brown-out reset has not occurred (set by firmware only) 0 = a brown-out reset occurred (must be set in firmware after a brown-out reset occurs) legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 75 pic18f2331/2431/4331/4431 6.0 flash program memory the flash program memory is readable, writable and erasable during normal operation over the entire v dd range. a read from program memory is executed on one byte at a time. a write to program memory is executed on blocks of 8 bytes at a time. program memory is erased in blocks of 64 bytes at a time. a bulk erase operation may not be issued from user code. while writing or erasing program memory, instruction fetches cease until the operation is complete. the program memory cannot be accessed during the write or erase, therefore, code cannot execute. an internal programming timer terminates program memory writes and erases. a value written to program memory does not need to be a valid instruction. executing a program memory location that forms an invalid instruction results in a nop . 6.1 table reads and table writes in order to read and write program memory, there are two operations that allow the processor to move bytes between the program memory space and the data ram:  table read ( tblrd )  table write ( tblwt ) the program memory space is 16-bits wide, while the data ram space is 8-bits wide. table reads and table writes move data between these two memory spaces through an 8-bit register (tablat). table read operations retrieve data from program memory and place it into tablat in the data ram space. figure 6-1 shows the operation of a table read with program memory and data ram. table write operations store data from tablat in the data memory space into holding registers in program memory. the procedure to write the contents of the holding registers into program memory is detailed in section 6.5 ?writing to flash program memory? . figure 6-2 shows the operation of a table write with program memory and data ram. table operations work with byte entities. a table block containing data, rather than program instructions, is not required to be word aligned. therefore, a table block can start and end at any byte address. if a table write is being used to write executable code into program memory, program instructions will need to be word aligned, (tblptrl<0> = 0 ). the eeprom on-chip timer controls the write and erase times. the write and erase voltages are gener- ated by an on-chip charge pump rated to operate over the voltage range of the device for byte or word operations. figure 6-1: table read operation table pointer (1) table latch (8-bit) program memory tblptrh tblptrl tablat tblptru instruction: tblrd * note 1: table pointer points to a byte in program memory. program memory (tblptr)
pic18f2331/2431/4331/4431 ds39616b-page 76 preliminary ? 2003 microchip technology inc. figure 6-2: table write operation 6.2 control registers several control registers are used in conjunction with the tblrd and tblwt instructions. these include the:  eecon1 register  eecon2 register  tablat register  tblptr registers 6.2.1 eecon1 and eecon2 registers eecon1 is the control register for memory accesses. eecon2 is not a physical register. reading eecon2 will read all ? 0 ?s. the eecon2 register is used exclusively in the memory write and erase sequences. control bit eepgd determines if the access will be to program or data eeprom memory. when clear, oper- ations will access the data eeprom memory. when set, program memory is accessed. control bit cfgs determines if the access will be to the configuration registers or to program memory/data eeprom memory. when set, subsequent operations access configuration registers. when cfgs is clear, the eepgd bit selects either program flash or data eeprom memory. the free bit controls program memory erase opera- tions. when the free bit is set, the erase operation is initiated on the next wr command. when free is clear, only writes are enabled. the wren bit enables and disables erase and write operations. when set, erase and write operations are allowed. when clear, erase and write operations are disabled ? the wr bit cannot be set while the wren bit is clear. this process helps to prevent accidental writes to memory due to errant (unexpected) code execution. firmware should keep the wren bit clear at all times, except when starting erase or write operations. once firmware has set the wr bit, the wren bit may be cleared. clearing the wren bit will not affect the operation in progress. the wrerr bit is set when a write operation is inter- rupted by a reset. in these situations, the user can check the wrerr bit and rewrite the location. it will be necessary to reload the data and address registers (eedata and eeadr) as these registers have cleared as a result of the reset. control bits rd and wr start read and erase/write operations, respectively. these bits are set by firm- ware, and cleared by hardware at the completion of the operation. the rd bit cannot be set when accessing program memory (eepgd = 1 ). program memory is read using table read instructions. see section 6.3 ?reading the flash program memory? regarding table reads. table pointer (1) table latch (8-bit) tblptrh tblptrl tablat program memory (tblptr) tblptru instruction: tblwt * note 1: table pointer actually points to one of eight holding registers, the address of which is determined by tblptrl<2:0>. the process for physically writing dat a to the program memory array is discussed in section 6.5 ?writing to flash program memory? . holding registers program memory note: interrupt flag bit eeif, in the pir2 register, is set when the write is complete. it must be cleared in software.
? 2003 microchip technology inc. preliminary ds39616b-page 77 pic18f2331/2431/4331/4431 register 6-1: eecon1 register r/w-x r/w-x u-0 r/w-0 r/w-x r/w-0 r/s-0 r/s-0 eepgd cfgs ? free wrerr wren wr rd bit 7 bit 0 bit 7 eepgd: flash program or data eeprom memory select bit 1 = access program flash memory 0 = access data eeprom memory bit 6 cfgs: flash program/data ee or configuration select bit 1 = access configuration registers 0 = access program flash or data eeprom memory bit 5 unimplemented: read as ? 0 ? bit 4 free: flash row erase enable bit 1 = erase the program memory row addressed by tblptr on the next wr command (cleared by completion of erase operation ? tblptr<5:0> are ignored) 0 = perform write only bit 3 wrerr: eeprom error flag bit 1 = a write operation was prematurely terminated (any reset during self-timed programming) 0 = the write operation completed normally note: when a wrerr occurs, the eepgd and cfgs bits are not cleared. this allows tracing of the error condition. bit 2 wren: write enable bit 1 = allows erase or write cycles 0 = inhibits erase or write cycles bit 1 wr: write control bit 1 = initiates a data eeprom erase/write cycle or a program memory erase cycle or write cycle. (the operation is self-timed and the bit is cleared by hardware once write is complete. the wr bit can only be set (not cleared) in software.) 0 = write cycle completed bit 0 rd: read control bit 1 = initiates a memory read (read takes one cycle. rd is cleared in hardware. the rd bit can only be set (not cleared) in software. rd bit cannot be set when eepgd = 1 .) 0 = read completed legend: r = readable bit s = settable only u = unimplemented bit, read as ?0? w = writable bit - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 78 preliminary ? 2003 microchip technology inc. 6.2.2 tablat ? table latch register the table latch (tablat) is an 8-bit register mapped into the sfr space. the table latch is used to hold 8-bit data during data transfers between program memory and data ram. 6.2.3 tblptr ? table pointer register the table pointer (tblptr) addresses a byte within the program memory. the tblptr is comprised of three sfr registers: table pointer upper byte, table pointer high byte and table pointer low byte (tblptru:tblptrh:tblptrl). these three regis- ters join to form a 22-bit wide pointer. the low order 21 bits allow the device to address up to 2 mbytes of pro- gram memory space. setting the 22nd bit allows access to the device id, the user id and the configuration bits. the tblptr is used by the tblrd and tblwt instruc- tions. these instructions can update the tblptr in one of four ways based on the table operation. these operations are shown in table 6-1. these operations on the tblptr only affect the low order 21 bits. 6.2.4 table pointer boundaries tblptr is used in reads, writes and erases of the flash program memory. when a tblrd is executed, all 22 bits of the table pointer determine which byte is read from program or configuration memory into tablat. when a tblwt is executed, the three lsbs of the table pointer (tblptr<2:0>) determine which of the eight program memory holding registers is written to. when the timed write to program memory (long write) begins, the 19 msbs of the table pointer, tblptr (tblptr<21:3>), will determine which program memory block of 8 bytes is written to (tblptr<2:0> are ignored). for more detail, see section 6.5 ?writing to flash program memory? . when an erase of program memory is executed, the 16 msbs of the table pointer (tblptr<21:6>) point to the 64-byte block that will be erased. the least significant bits (tblptr<5:0>) are ignored. figure 6-3 describes the relevant boundaries of tblptr based on flash program memory operations. table 6-1: table pointer operations with tblrd and tblwt instructions figure 6-3: table pointer boundaries based on operation example operation on table pointer tblrd* tblwt* tblptr is not modified tblrd*+ tblwt*+ tblptr is incremented after the read/write tblrd*- tblwt*- tblptr is decremented after the read/write tblrd+* tblwt+* tblptr is incremented before the read/write 21 16 15 87 0 erase ? tblptr<21:6> long write ? tblptr<21:3> read or write ? tblptr<21:0> tblptrl tblptrh tblptru
? 2003 microchip technology inc. preliminary ds39616b-page 79 pic18f2331/2431/4331/4431 6.3 reading the flash program memory the tblrd instruction is used to retrieve data from program memory and placed into data ram. table reads from program memory are performed one byte at a time. tblptr points to a byte address in program space. executing a tblrd instruction places the byte pointed to into tablat. in addition, tblptr can be modified automatically for the next table read operation. the internal program memory is typically organized by words. the least significant bit of the address selects between the high and low bytes of the word. figure 6-4 shows the interface between the internal program memory and the tablat. figure 6-4: reads from flash program memory example 6-1: reading a flash program memory word odd (high) byte program memory even (low) byte tablat tblptr instruction register (ir) read register lsb = 1 tblptr lsb = 0 movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the word movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl read_word tblrd*+ ; read into tablat and increment tblptr movfw tablat ; get data movwf word_even tblrd*+ ; read into tablat and increment tblptr movfw tablat ; get data movwf word_odd
pic18f2331/2431/4331/4431 ds39616b-page 80 preliminary ? 2003 microchip technology inc. 6.4 erasing flash program memory the minimum erase block size is 32 words or 64 bytes under firmware control. only through the use of an external programmer, or through icsp control can larger blocks of program memory be bulk erased. word erase in flash memory is not supported. when initiating an erase sequence from the micro- controller itself, a block of 64 bytes of program memory is erased. the most significant 16 bits of the tblptr<21:6> point to the block being erased. tblptr<5:0> are ignored. the eecon1 register commands the erase operation. the eepgd bit must be set to point to the flash pro- gram memory. the cfgs bit must be clear to access program flash and data eeprom memory. the wren bit must be set to enable write operations. the free bit is set to select an erase operation. the wr bit is set as part of the required instruction sequence (as shown in example 6-2), and starts the actual erase operation. it is not necessary to load the tablat register with any data, as it is ignored. for protection, the write initiate sequence using eecon2 must be used. a long write is necessary for erasing the internal flash. instruction execution is halted while in a long write cycle. the long write will be terminated by the internal programming timer. 6.4.1 flash program memory erase sequence the sequence of events for erasing a block of internal program memory location is: 1. load table pointer with address of row being erased. 2. set the eecon1 register for the erase operation: - set eepgd bit to point to program memory; - clear the cfgs bit to access program memory; - set wren bit to enable writes; - set free bit to enable the erase. 3. disable interrupts. 4. write 55h to eecon2. 5. write aah to eecon2. 6. set the wr bit. this will begin the row erase cycle. 7. the cpu will stall for duration of the erase (about 2 ms using internal timer). 8. execute a nop . 9. re-enable interrupts. example 6-2: erasing a flash program memory row movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low movwf tblptrl erase_row bsf eecon1,eepgd ; point to flash program memory bsf eecon1,wren ; enable write to memory bsf eecon1,free ; enable row erase operation bcf intcon,gie ; disable interrupts movlw 55h movwf eecon2 ; write 55h required movlw aah sequence movwf eecon2 ; write aah bsf eecon2,wr ; start erase (cpu stall) nop bsf intcon,gie ; re-enable interrupts
? 2003 microchip technology inc. preliminary ds39616b-page 81 pic18f2331/2431/4331/4431 6.5 writing to flash program memory the programming block size is 4 words or 8 bytes. word or byte programming is not supported. table writes are used internally to load the holding registers needed to program the flash memory. there are 8 holding registers used by the table writes for programming. since the table latch (tablat) is only a single byte, the tblwt instruction has to be executed 8 times for each programming operation. all of the table write operations will essentially be short writes, because only the holding registers are written. at the end of updating 8 registers, the eecon1 register must be written to, to start the programming operation with a long write. the long write is necessary for programming the internal flash. instruction execution is halted while in a long write cycle. the long write will be terminated by the internal programming timer. figure 6-5: table writes to flash program memory 6.5.1 flash program memory write sequence the sequence of events for programming an internal program memory location should be: 1. read 64 bytes into ram. 2. update data values in ram as necessary. 3. load table pointer with address being erased. 4. do the row erase procedure (see section 6.4.1 ?flash program memory erase sequence? ). 5. load table pointer with address of first byte being written. 6. write the first 8 bytes into the holding registers with auto-increment. 7. set the eecon1 register for the write operation: - set eepgd bit to point to program memory; - clear the cfgs bit to access program memory; - set wren bit to enable byte writes. 8. disable interrupts. 9. write 55h to eecon2. 10. write aah to eecon2. 11. set the wr bit. this will begin the write cycle. 12. the cpu will stall for duration of the write (about 2 ms using internal timer). 13. execute a nop . 14. re-enable interrupts. 15. repeat steps 6-14 seven times, to write 64 bytes. 16. verify the memory (table read). this procedure will require about 18 ms to update one row of 64 bytes of memory. an example of the required code is given in example 6-3. holding register tablat holding register tblptr = xxxxx7 holding register tblptr = xxxxx1 holding register tblptr = xxxxx0 8 8 8 8 write register tblptr = xxxxx2 program memory
pic18f2331/2431/4331/4431 ds39616b-page 82 preliminary ? 2003 microchip technology inc. example 6-3: writing to flash program memory movlw d'64 ; number of bytes in erase block movwf counter movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low ; 6 lsb = 0 movwf tblptrl read_block tblrd*+ ; read into tablat, and inc movfw tablat ; get data movwf postinc0 ; store data and increment fsr0 decfsz counter ; done? goto read_block ; repeat modify_word movlw data_addr_high ; point to buffer movwf fsr0h movlw data_addr_low movwf fsr0l movlw new_data_low ; update buffer word and increment fsr0 movwf postinc0 movlw new_data_high ; update buffer word movwf indf0 erase_block movlw code_addr_upper ; load tblptr with the base movwf tblptru ; address of the memory block movlw code_addr_high movwf tblptrh movlw code_addr_low ; 6 lsb = 0 movwf tblptrl bcf eecon1,cfgs ; point to prog/eeprom memory bsf eecon1,eepgd ; point to flash program memory bsf eecon1,wren ; enable write to memory bsf eecon1,free ; enable row erase operation bcf intcon,gie ; disable interrupts movlw 55h ; required sequence movwf eecon2 ; write 55h movlw aah movwf eecon2 ; write aah bsf eecon1,wr ; start erase (cpu stall) nop bsf intcon,gie ; re-enable interrupts write_buffer_back movlw 8 ; number of write buffer groups of 8 bytes movwf counter_hi movlw buffer_addr_high ; point to buffer movwf fsr0h movlw buffer_addr_low movwf fsr0l program_loop movlw 8 ; number of bytes in holding register movwf counter write_word_to_hregs movfw postinc0 ; get low byte of buffer data and increment fsr0 movwf tablat ; present data to table latch tblwt+* ; short write ; to internal tblwt holding register, increment ; tblptr decfsz counter ; loop until buffers are full goto write_word_to_hregs
? 2003 microchip technology inc. preliminary ds39616b-page 83 pic18f2331/2431/4331/4431 example 6-3: writing to flash program memory (continued) 6.5.2 write verify depending on the application, good programming practice may dictate that the value written to the memory should be verified against the original value. this should be used in applications where excessive writes can stress bits near the specification limit. 6.5.3 unexpected termination of write operation if a write is terminated by an unplanned event, such as loss of power or an unexpected reset, the memory location just programmed should be verified and repro- grammed if needed. the wrerr bit is set when a write operation is interrupted by a mclr reset, or a wdt time-out reset during normal operation. in these situations, users can check the wrerr bit and rewrite the location. 6.6 flash program operation during code protection see section 22.5 ?program verification and code protection? for details on code protection of flash pro- gram memory. table 6-2: registers associated with program flash memory program_memory bcf intcon,gie ; disable interrupts movlw 55h ; required sequence movwf eecon2 ; write 55h movlw aah movwf eecon2 ; write aah bsf eecon1,wr ; start program (cpu stall) nop bsf intcon, gie ; re-enable interrupts decfsz counter_hi ; loop until done goto program_loop bcf eecon1, wren ; disable write to memory name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets tblptru ? ? bit21 program memory table pointer upper byte (tblptr<20:16>) --00 0000 --00 0000 tbpltrh program memory table pointer high byte (tblptr<15:8>) 0000 0000 0000 0000 tblptrl program memory table pointer high byte (tblptr<7:0>) 0000 0000 0000 0000 tablat program memory table latch 0000 0000 0000 0000 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if intf rbif 0000 000x 0000 000u eecon2 eeprom control register2 (not a physical register) ? ? eecon1 eepgd cfgs ? free wrerr wren wr rd xx-0 x000 uu-0 u000 ipr2 osfip ? ?eeip ?lvdip ? ccp2ip 1--1 -1-1 1--1 -1-1 pir2 osfif ? ?eeif ?lvdif ? ccp2if 0--0 -0-0 0--0 -0-0 pie2 osfie ? ?eeie ?lvdie ? ccp2ie 0--0 -0-0 0--0 -0-0 legend: x = unknown, u = unchanged, r = reserved, - = unimplemented, read as ? 0 ?. shaded cells are not used during flash/eeprom access.
pic18f2331/2431/4331/4431 ds39616b-page 84 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 85 pic18f2331/2431/4331/4431 7.0 data eeprom memory the data eeprom is readable and writable during normal operation over the entire v dd range. the data memory is not directly mapped in the register file space. instead, it is indirectly addressed through the special function registers (sfr). there are four sfrs used to read and write the program and data eeprom memory. these registers are:  eecon1  eecon2  eedata  eeadr the eeprom data memory allows byte read and write. when interfacing to the data memory block, eedata holds the 8-bit data for read/write and eeadr holds the address of the eeprom location being accessed. these devices have 256 bytes of data eeprom with an address range from 00h to ffh. the eeprom data memory is rated for high erase/ write cycle endurance. a byte write automatically erases the location and writes the new data (erase- before-write). the write time is controlled by an on-chip timer. the write time will vary with voltage and temper- ature, as well as from chip-to-chip. please refer to parameter d122 (table 25-1 in section 25.0 ?electri- cal characteristics? ) for exact limits. 7.1 eeadr the address register can address 256 bytes of data eeprom. 7.2 eecon1 and eecon2 registers eecon1 is the control register for memory accesses. eecon2 is not a physical register. reading eecon2 will read all ? 0 ?s. the eecon2 register is used exclusively in the memory write and erase sequences. control bit eepgd determines if the access will be to program or data eeprom memory. when clear, oper- ations will access the data eeprom memory. when set, program memory is accessed. control bit cfgs determines if the access will be to the configuration registers or to program memory/data eeprom memory. when set, subsequent operations access configuration registers. when cfgs is clear, the eepgd bit selects either program flash or data eeprom memory. the wren bit enables and disables erase and write operations. when set, erase and write operations are allowed. when clear, erase and write operations are disabled; the wr bit cannot be set while the wren bit is clear. this mechanism helps to prevent accidental writes to memory due to errant (unexpected) code execution. firmware should keep the wren bit clear at all times, except when starting erase or write operations. once firmware has set the wr bit, the wren bit may be cleared. clearing the wren bit will not affect the operation in progress. the wrerr bit is set when a write operation is interrupted by a reset. in these situations, the user can check the wrerr bit and rewrite the location. it is necessary to reload the data and address registers (eedata and eeadr), as these registers have cleared as a result of the reset. control bits rd and wr start read and erase/write operations, respectively. these bits are set by firm- ware, and cleared by hardware at the completion of the operation. the rd bit cannot be set when accessing program memory (eepgd = 1 ). program memory is read using table read instructions. see section 6.1 ?table reads and table writes? regarding table reads. note: interrupt flag bit, eeif in the pir2 register, is set when write is complete. it must be cleared in software.
pic18f2331/2431/4331/4431 ds39616b-page 86 preliminary ? 2003 microchip technology inc. register 7-1: eecon1 register r/w-x r/w-x u-0 r/w-0 r/w-x r/w-0 r/s-0 r/s-0 eepgd cfgs ? free wrerr wren wr rd bit 7 bit 0 bit 7 eepgd: flash program or data eeprom memory select bit 1 = access program flash memory 0 = access data eeprom memory bit 6 cfgs: flash program/data ee or configuration select bit 1 = access configuration or calibration registers 0 = access program flash or data eeprom memory bit 5 unimplemented: read as ? 0 ? bit 4 free: flash row erase enable bit 1 = erase the program memory row addressed by tblptr on the next wr command (cleared by completion of erase operation) 0 = perform write only bit 3 wrerr: eeprom error flag bit 1 = a write operation was prematurely terminated (mclr or wdt reset during self-timed erase or program operation) 0 = the write operation completed normally note: when a wrerr occurs, the eepgd or free bits are not cleared. this allows trac- ing of the error condition. bit 2 wren: erase/write enable bit 1 = allows erase/write cycles 0 = inhibits erase/write cycles bit 1 wr: write control bit 1 = initiates a data eeprom erase/write cycle or a program memory erase cycle or write cycle. (the operation is self-timed and the bit is cleared by hardware once write is complete. the wr bit can only be set (not cleared) in software.) 0 = write cycle is completed bit 0 rd: read control bit 1 = initiates a memory read (read takes one cycle. rd is cleared in hardware. the rd bit can only be set (not cleared) in software. rd bit cannot be set when eepgd = 1 .) 0 = read completed legend: r = readable bit s = settable only u = unimplemented bit, read as ?0? w = writable bit - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 87 pic18f2331/2431/4331/4431 7.3 reading the data eeprom memory to read a data memory location, the user must write the address to the eeadr register, clear the eepgd con- trol bit (eecon1<7>) and then set control bit rd (eecon1<0>). the data is available for the very next instruction cycle; therefore, the eedata register can be read by the next instruction. eedata will hold this value until another read operation, or until it is written to by the user (during a write operation). 7.4 writing to the data eeprom memory to write an eeprom data location, the address must first be written to the eeadr register and the data writ- ten to the eedata register. the sequence in example 7-2 must be followed to initiate the write cycle. the write will not begin if this sequence is not exactly followed (write 55h to eecon2, write aah to eecon2, then set wr bit) for each byte. it is strongly recom- mended that interrupts be disabled during this code segment. additionally, the wren bit in eecon1 must be set to enable writes. this mechanism prevents accidental writes to data eeprom due to unexpected code exe- cution (i.e., runaway programs). the wren bit should be kept clear at all times, except when updating the eeprom. the wren bit is not cleared by hardware. after a write sequence has been initiated, eecon1, eeadr and eedata cannot be modified. the wr bit will be inhibited from being set unless the wren bit is set. the wren bit must be set on a previous instruc- tion. both wr and wren cannot be set with the same instruction. at the completion of the write cycle, the wr bit is cleared in hardware and the eeprom interrupt flag bit (eeif) is set. the user may either enable this interrupt or poll this bit. eeif must be cleared by software. 7.5 write verify depending on the application, good programming practice may dictate that the value written to the mem- ory should be verified against the original value. this should be used in applications where excessive writes can stress bits near the specification limit. 7.6 protection against spurious write there are conditions when the device may not want to write to the data eeprom memory. to protect against spurious eeprom writes, various mechanisms have been built-in. on power-up, the wren bit is cleared. also, the power-up timer (72 ms duration) prevents eeprom write. the write initiate sequence and the wren bit together help prevent an accidental write during brown-out, power glitch, or software malfunction. example 7-1: data eeprom read example 7-2: data eeprom write movlw data_ee_addr ; movwf eeadr ; data memory address to read bcf eecon1, eepgd ; point to data memory bsf eecon1, rd ; eeprom read movf eedata, w ; w = eedata movlw data_ee_addr ; movwf eeadr ; data memory address to write movlw data_ee_data ; movwf eedata ; data memory value to write bcf eecon1, eepgd ; point to data memory bsf eecon1, wren ; enable writes bcf intcon, gie ; disable interrupts movlw 55h ; required movwf eecon2 ; write 55h sequence movlw aah ; movwf eecon2 ; write aah bsf eecon1, wr ; set wr bit to begin write bsf intcon, gie ; enable interrupts sleep ; wait for interrupt to signal write complete bcf eecon1, wren ; disable writes
pic18f2331/2431/4331/4431 ds39616b-page 88 preliminary ? 2003 microchip technology inc. 7.7 operation during code-protect data eeprom memory has its own code-protect bits in configuration words. external read and write opera- tions are disabled if either of these mechanisms are enabled. the microcontroller itself can both read and write to the internal data eeprom, regardless of the state of the code-protect configuration bit. refer to section 22.0 ?special features of the cpu? for additional information. 7.8 using the data eeprom the data eeprom is a high-endurance, byte addressable array that has been optimized for the storage of frequently changing information (e.g., program variables or other data that are updated often). frequently changing values will typically be updated more often than specification d124 or d124a. if this is not the case, an array refresh must be performed. for this reason, variables that change infrequently (such as constants, ids, calibration, etc.) should be stored in flash program memory. a simple data eeprom refresh routine is shown in example 7-3. example 7-3: data eeprom refresh routine table 7-1: registers associated with data eeprom memory note: if data eeprom is only used to store con- stants and/or data that changes rarely, an array refresh is likely not required. see specification d124 or d124a. clrf eeadr ; start at address 0 bcf eecon1, cfgs ; set for memory bcf eecon1, eepgd ; set for data eeprom bcf intcon, gie ; disable interrupts bsf eecon1, wren ; enable writes loop ; loop to refresh array bsf eecon1, rd ; read current address movlw 55h ; movwf eecon2 ; write 55h movlw aah ; movwf eecon2 ; write aah bsf eecon1, wr ; set wr bit to begin write btfsc eecon1, wr ; wait for write to complete bra $-2 incfsz eeadr, f ; increment address bra loop ; not zero, do it again bcf eecon1, wren ; disable writes bsf intcon, gie ; enable interrupts name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie inte rbie tmr0if intf rbif 0000 000x 0000 000u eeadr eeprom address register 0000 0000 0000 0000 eedata eeprom data register 0000 0000 0000 0000 eecon2 eeprom control register2 (not a physical register) ? ? eecon1 eepgd cfgs ? free wrerr wren wr rd xx-0 x000 uu-0 u000 ipr2 osfip ? ?eeip ?lvdip ? ccp2ip 1--1 -1-1 1--1 -1-1 pir2 osfif ? ?eeif ?lvdif ? ccp2if 0--0 -0-0 0--0 -0-0 pie2 osfie ? ?eeie ?lvdie ? ccp2ie 0--0 -0-0 0--0 -0-0 legend: x = unknown, u = unchanged, r = reserved, - = unimplemented, read as ? 0 ?. shaded cells are not used during flash/eeprom access.
? 2003 microchip technology inc. preliminary ds39616b-page 89 pic18f2331/2431/4331/4431 8.0 8 x 8 hardware multiplier 8.1 introduction an 8 x 8 hardware multiplier is included in the alu of the pic18f2331/2431/4331/4431 devices. by making the multiply a hardware operation, it completes in a single instruction cycle. this is an unsigned multiply that gives a 16-bit result. the result is stored into the 16-bit product register pair (prodh:prodl). the multiplier does not affect any flags in the status register. making the 8 x 8 multiplier execute in a single cycle gives the following advantages:  higher computational throughput  reduces code size requirements for multiply algorithms the performance increase allows the device to be used in applications previously reserved for digital signal processors. table 8-1 shows a performance comparison between enhanced devices using the single cycle hardware mul- tiply, and performing the same function without the hardware multiply. table 8-1: performance comparison 8.2 operation example 8-1 shows the sequence to do an 8 x 8 unsigned multiply. only one instruction is required when one argument of the multiply is already loaded in the wreg register. example 8-2 shows the sequence to do an 8 x 8 signed multiply. to account for the sign bits of the arguments, each argument?s most significant bit (msb) is tested and the appropriate subtractions are done. example 8-1: 8 x 8 unsigned multiply routine example 8-2: 8 x 8 signed multiply routine routine multiply method program memory (words) cycles (max) time @ 40 mhz @ 10 mhz @ 4 mhz 8 x 8 unsigned without hardware multiply 13 69 6.9 s 27.6 s 69 s hardware multiply 1 1 100 ns 400 ns 1 s 8 x 8 signed without hardware multiply 33 91 9.1 s 36.4 s 91 s hardware multiply 6 6 600 ns 2.4 s6 s 16 x 16 unsigned without hardware multiply 21 242 24.2 s 96.8 s 242 s hardware multiply 24 24 2.4 s9.6 s 24 s 16 x 16 signed without hardware multiply 52 254 25.4 s102.6 s 254 s hardware multiply 36 36 3.6 s 14.4 s 36 s movf arg1, w ; mulwf arg2 ; arg1 * arg2 -> ; prodh:prodl movf arg1, w mulwf arg2 ; arg1 * arg2 -> ; prodh:prodl btfsc arg2, sb ; test sign bit subwf prodh, f ; prodh = prodh ; - arg1 movf arg2, w btfsc arg1, sb ; test sign bit subwf prodh, f ; prodh = prodh ; - arg2
pic18f2331/2431/4331/4431 ds39616b-page 90 preliminary ? 2003 microchip technology inc. example 8-3 shows the sequence to do a 16 x 16 unsigned multiply. equation 8-1 shows the algorithm that is used. the 32-bit result is stored in four registers, res3:res0. equation 8-1: 16 x 16 unsigned multiplication algorithm example 8-3: 16 x 16 unsigned multiply routine example 8-4 shows the sequence to do a 16 x 16 signed multiply. equation 8-2 shows the algorithm used. the 32-bit result is stored in four registers, res3:res0. to account for the sign bits of the argu- ments, each argument pair?s most significant bit (msb) is tested, and the appropriate subtractions are done. equation 8-2: 16 x 16 signed multiplication algorithm example 8-4: 16 x 16 signed multiply routine movfarg1l, w mulwfarg2l ; arg1l * arg2l -> ; prodh:prodl movffprodh, res1 ; movffprodl, res0 ; ; movfarg1h, w mulwfarg2h ; arg1h * arg2h -> ; prodh:prodl movffprodh, res3 ; movffprodl, res2 ; ; movfarg1l, w mulwfarg2h ; arg1l * arg2h -> ; prodh:prodl movfprodl, w ; addwfres1, f ; add cross movfprodh, w ; products addwfcres2, f ; clrfwreg ; addwfcres3, f ; ; movfarg1h, w ; mulwfarg2l ; arg1h * arg2l -> ; prodh:prodl movfprodl, w ; addwfres1, f ; add cross movfprodh, w ; products addwfcres2, f ; clrfwreg ; addwfcres3, f ; res3:res0 = arg1h:arg1l ? arg2h:arg2l = (arg1h ? arg2h ? 2 16 )+ (arg1h ? arg2l ? 2 8 )+ (arg1l ? arg2h ? 2 8 )+ (arg1l ? arg2l) movf arg1l, w mulwf arg2l ; arg1l * arg2l -> ; prodh:prodl movff prodh, res1 ; movff prodl, res0 ; ; movf arg1h, w mulwf arg2h ; arg1h * arg2h -> ; prodh:prodl movff prodh, res3 ; movff prodl, res2 ; ; movf arg1l, w mulwf arg2h ; arg1l * arg2h -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; movf arg1h, w ; mulwf arg2l ; arg1h * arg2l -> ; prodh:prodl movf prodl, w ; addwf res1, f ; add cross movf prodh, w ; products addwfc res2, f ; clrf wreg ; addwfc res3, f ; ; btfss arg2h, 7 ; arg2h:arg2l neg? bra sign_arg1 ; no, check arg1 movf arg1l, w ; subwf res2 ; movf arg1h, w ; subwfb res3 ; sign_arg1 btfss arg1h, 7 ; arg1h:arg1l neg? bra cont_code ; no, done movf arg2l, w ; subwf res2 ; movf arg2h, w ; subwfb res3 ; cont_code : res3:res0 = arg1h:arg1l ? arg2h:arg2l = (arg1h ? arg2h ? 2 16 )+ (arg1h ? arg2l ? 2 8 )+ (arg1l ? arg2h 2 2 8 )+ (arg1l ? arg2l)+ (-1 ? arg2h<7> ? arg1h:arg1l ? 2 16 )+ (-1 ? arg1h<7> ? arg2h:arg2l ? 2 16 )
? 2003 microchip technology inc. preliminary ds39616b-page 91 pic18f2331/2431/4331/4431 9.0 interrupts the pic18f2331/2431/4331/4431 devices have multiple interrupt sources and an interrupt priority feature that allows each interrupt source to be assigned a high priority level or a low priority level. the high priority interrupt vector is at 000008h and the low priority interrupt vector is at 000018h. high priority interrupt events will interrupt any low priority interrupts that may be in progress. there are ten registers which are used to control interrupt operation. these registers are:  rcon intcon  intcon2  intcon3  pir1, pir2, pir3  pie1, pie2, pie3  ipr1, ipr2, ipr3 it is recommended that the microchip header files sup- plied with mplab ? ide be used for the symbolic bit names in these registers. this allows the assembler/ compiler to automatically take care of the placement of these bits within the specified register. in general, each interrupt source has three bits to con- trol its operation. the functions of these bits are:  flag bit to indicate that an interrupt event occurred  enable bit that allows program execution to branch to the interrupt vector address when the flag bit is set  priority bit to select high priority or low priority (most interrupt sources have priority bits) the interrupt priority feature is enabled by setting the ipen bit (rcon<7>). when interrupt priority is enabled, there are two bits which enable interrupts globally. setting the gieh bit (intcon<7>) enables all interrupts that have the priority bit set (high priority). setting the giel bit (intcon<6>) enables all interrupts that have the priority bit cleared (low priority). when the interrupt flag, enable bit and appropriate global interrupt enable bit are set, the interrupt will vector immediately to address 000008h or 000018h, depending on the priority bit setting. individual interrupts can be disabled through their corresponding enable bits. when the ipen bit is cleared (default state), the inter- rupt priority feature is disabled and interrupts are com- patible with picmicro ? mid-range devices. in compatibility mode, the interrupt priority bits for each source have no effect. intcon<6> is the peie bit, which enables/disables all peripheral interrupt sources. intcon<7> is the gie bit, which enables/disables all interrupt sources. all interrupts branch to address 000008h in compatibility mode. when an interrupt is responded to, the global interrupt enable bit is cleared to disable further interrupts. if the ipen bit is cleared, this is the gie bit. if interrupt priority levels are used, this will be either the gieh or giel bit. high priority interrupt sources can interrupt a low priority interrupt. low priority interrupts are not processed while high priority interrupts are in progress. the return address is pushed onto the stack and the pc is loaded with the interrupt vector address (000008h or 000018h). once in the interrupt service routine, the source(s) of the interrupt can be determined by polling the interrupt flag bits. the interrupt flag bits must be cleared in software before re- enabling interrupts to avoid recursive interrupts. the ?return from interrupt? instruction, retfie , exits the interrupt routine and sets the gie bit (gieh or giel if priority levels are used), which re-enables interrupts. for external interrupt events, such as the int pins or the portb input change interrupt, the interrupt latency will be three to four instruction cycles. the exact latency is the same for one or two-cycle instructions. individual interrupt flag bits are set, regardless of the status of their corresponding enable bit or the gie bit. note: do not use the movff instruction to modify any of the interrupt control registers while any interrupt is enabled. doing so may cause erratic microcontroller behavior.
pic18f2331/2431/4331/4431 ds39616b-page 92 preliminary ? 2003 microchip technology inc. figure 9-1: interrupt logic tmr0ie gieh/gie giel/peie wake-up if in interrupt to cpu vector to location 0008h int2if int2ie int2ip int1if int1ie int1ip tmr0if tmr0ie tmr0ip int0if int0ie rbif rbie rbip ipen tmr0if tmr0ip int1if int1ie int1ip int2if int2ie int2ip rbif rbie rbip int0if int0ie giel\peie interrupt to cpu vector to location ipen ipe 0018h pspif pspie pspip pspif pspie pspip adif adie adip rcif rcie rcip additional peripheral interrupts adif adie adip high priority interrupt generation low priority interrupt generation rcif rcie rcip additional peripheral interrupts power-managed mode
? 2003 microchip technology inc. preliminary ds39616b-page 93 pic18f2331/2431/4331/4431 9.1 intcon registers the intcon registers are readable and writable registers, which contain various enable, priority and flag bits. register 9-1: intcon register note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling. r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-x gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif bit 7 bit 0 bit 7 gie/gieh: global interrupt enable bit when ipen = 0 : 1 = enables all unmasked interrupts 0 = disables all interrupts when ipen = 1 : 1 = enables all high priority interrupts 0 = disables all high priority interrupts bit 6 peie/giel: peripheral interrupt enable bit when ipen = 0 : 1 = enables all unmasked peripheral interrupts 0 = disables all peripheral interrupts when ipen = 1 : 1 = enables all low priority peripheral interrupts 0 = disables all low priority peripheral interrupts bit 5 tmr0ie: tmr0 overflow interrupt enable bit 1 = enables the tmr0 overflow interrupt 0 = disables the tmr0 overflow interrupt bit 4 int0ie: int0 external interrupt enable bit 1 = enables the int0 external interrupt 0 = disables the int0 external interrupt bit 3 rbie: rb port change interrupt enable bit 1 = enables the rb port change interrupt for rb7:rb4 pins 0 = disables the rb port change interrupt for rb7:rb4 pins bit 2 tmr0if: tmr0 overflow interrupt flag bit 1 = tmr0 register has overflowed (must be cleared in software) 0 = tmr0 register did not overflow bit 1 int0if: int0 external interrupt flag bit 1 = the int0 external interrupt occurred (must be cleared in software) 0 = the int0 external interrupt did not occur bit 0 rbif: rb port change interrupt flag bit 1 = at least one of the rb7:rb4 pins changed state (must be cleared in software) 0 = none of the rb7:rb4 pins have changed state note: a mismatch condition will continue to set this bit. reading portb will end the mismatch condition and allow the bit to be cleared. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 94 preliminary ? 2003 microchip technology inc. register 9-2: intcon2 register r/w-1 r/w-1 r/w-1 r/w-1 u-0 r/w-1 u-0 r/w-1 rbpu intedg0 intedg1 intedg2 ?tmr0ip ?rbip bit 7 bit 0 bit 7 rbpu : portb pull-up enable bit 1 = all portb pull-ups are disabled 0 = portb pull-ups are enabled by individual port latch values bit 6 intedg0: external interrupt0 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 5 intedg1: external interrupt1 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 4 intedg2: external interrupt2 edge select bit 1 = interrupt on rising edge 0 = interrupt on falling edge bit 3 unimplemented: read as ? 0 ? bit 2 tmr0ip: tmr0 overflow interrupt priority bit 1 = high priority 0 = low priority bit 1 unimplemented: read as ? 0 ? bit 0 rbip: rb port change interrupt priority bit 1 = high priority 0 = low priority legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling.
? 2003 microchip technology inc. preliminary ds39616b-page 95 pic18f2331/2431/4331/4431 register 9-3: intcon3 register r/w-1 r/w-1 u-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 int2ip int1ip ?int2ieint1ie ? int2if int1if bit 7 bit 0 bit 7 int2ip: int2 external interrupt priority bit 1 = high priority 0 = low priority bit 6 int1ip: int1 external interrupt priority bit 1 = high priority 0 = low priority bit 5 unimplemented: read as ? 0 ? bit 4 int2ie: int2 external interrupt enable bit 1 = enables the int2 external interrupt 0 = disables the int2 external interrupt bit 3 int1ie: int1 external interrupt enable bit 1 = enables the int1 external interrupt 0 = disables the int1 external interrupt bit 2 unimplemented: read as ? 0 ? bit 1 int2if: int2 external interrupt flag bit 1 = the int2 external interrupt occurred (must be cleared in software) 0 = the int2 external interrupt did not occur bit 0 int1if: int1 external interrupt flag bit 1 = the int1 external interrupt occurred (must be cleared in software) 0 = the int1 external interrupt did not occur legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown note: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit. user software should ensure the appropriate interrupt flag bits are clear prior to enabling an interrupt. this feature allows for software polling.
pic18f2331/2431/4331/4431 ds39616b-page 96 preliminary ? 2003 microchip technology inc. 9.2 pir registers the pir registers contain the individual flag bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are two peripheral interrupt flag registers (pir1, pir2). register 9-4: pir1: peripheral interrupt request (flag) register 1 note 1: interrupt flag bits are set when an interrupt condition occurs, regardless of the state of its corresponding enable bit or the global enable bit, gie (intcon<7>). 2: user software should ensure the appropri- ate interrupt flag bits are cleared prior to enabling an interrupt, and after servicing that interrupt. u-0 r/w-0 r-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 ? adif rcif txif sspif ccp1if tmr2if tmr1if bit 7 bit 0 bit 7 unimplemented: read as ? 0 ?. bit 6 adif: a/d converter interrupt flag bit 1 = an a/d conversion completed (must be cleared in software) 0 = the a/d conversion is not complete bit 5 rcif: usart receive interrupt flag bit 1 = the usart receive buffer, rcreg, is full (cleared when rcreg is read) 0 = the usart receive buffer is empty bit 4 txif: usart transmit interrupt flag bit 1 = the usart transmit buffer, txreg, is empty (cleared when txreg is written) 0 = the usart transmit buffer is full bit 3 sspif: synchronous serial port interrupt flag bit 1 = the transmission/reception is complete (must be cleared in software) 0 = waiting to transmit/receive bit 2 ccp1if: ccp1 interrupt flag bit capture mode: 1 = a tmr1 register capture occurred (must be cleared in software) 0 = no tmr1 register capture occurred compare mode: 1 = a tmr1 register compare match occurred (must be cleared in software) 0 = no tmr1 register compare match occurred pwm mode: unused in this mode bit 1 tmr2if: tmr2 to pr2 match interrupt flag bit 1 = tmr2 to pr2 match occurred (must be cleared in software) 0 = no tmr2 to pr2 match occurred bit 0 tmr1if: tmr1 overflow interrupt flag bit 1 = tmr1 register overflowed (must be cleared in software) 0 = tmr1 register did not overflow note 1: this bit is reserved on pic18f2x31 devices; always maintain this bit clear. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 97 pic18f2331/2431/4331/4431 register 9-5: pir2: peripheral interrupt request (flag) register 2 r/w-0 u-0 u-0 r/w-0 u-0 r/w-0 u-0 r/w-0 osfif ? ? eeif ?lvdif ? ccp2if bit 7 bit 0 bit 7 osfif: oscillator fail interrupt flag bit 1 = system oscillator failed, clock input has changed to intosc (must be cleared in software) 0 = system clock operating bit 6-5 unimplemented: read as ? 0 ? bit 4 eeif: eeprom or flash write operation interrupt flag bit 1 = the write operation is complete (must be cleared in software) 0 = the write operation is not complete or has not been started bit 3 unimplemented: read as ? 0 ? bit 2 lvdif: low-voltage detect interrupt flag bit 1 = the supply voltage has fallen below the specified lvd voltage (must be cleared in software) 0 = the supply voltage is greater than the specified lvd voltage bit 1 unimplemented: read as ? 0 ? bit 0 ccp2if: ccp2 interrupt flag bit capture mode : 1 = a tmr1 register capture occurred (must be cleared in software) 0 = no tmr1 register capture occurred compare mode : 1 = a tmr1 register compare match occurred (must be cleared in software) 0 = no tmr1 register compare match occurred pwm mode : not used in pwm mode legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 98 preliminary ? 2003 microchip technology inc. register 9-6: pir3: peripheral interrupt flag register 3 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? ? ptif ic3drif ic2qeif ic1if tmr5if bit 7 bit 0 bit 7-5 unimplemented: read as ? 0 ? bit 4 ptif: pwm time base interrupt bit 1 = pwm time base matched the value in ptper register. interrupt is issued according to the postscaler settings. ptif must be cleared in software. 0 = pwm time base has not matched the value in ptper register. bit 3 ic3drif: ic3 interrupt flag/direction change interrupt flag bit ic3 enabled (cap3con<3:0>) 1 = tmr5 value was captured by the active edge on cap3 input (must be cleared in software). 0 = tmr5 capture has not occurred. qei enabled (qeim<2:0>) 1 = direction of rotation has changed (must be cleared in software). 0 = direction of rotation has not changed. bit 2 ic2qeif: ic2 interrupt flag/qei interrupt flag bit ic2 enabled (cap2con<3:0>) 1 = tmr5 value was captured by the active edge on cap2 input (must be cleared in software). 0 = tmr5 capture has not occurred. qei enabled (qeim<2:0>) 1 = the qei position counter has reached the maxcnt value or the index pulse, indx, has been detected. depends on the qei operating mode enabled. must be cleared in software. 0 = the qei position counter has not reached the maxcnt value or the index pulse has not been detected. bit 1 ic1if: ic1 interrupt flag bit ic1 enabled (cap1con<3:0>) 1 = tmr5 value was captured by the active edge on cap1 input (must be cleared in software). 0 = tmr5 capture has not occurred. qei enabled (qeim<2:0>) and velocity measurement mode enabled (velm = 0 in qeicon register) 1 = timer5 value was captured by the active velocity edge (based on pha or phb input). cap1ren bit must be set in cap1con register. ic1if must be cleared in software. 0 = timer5 value was not captured by the active velocity edge. bit 0 tmr5if: timer5 interrupt flag bit 1 = timer5 time base matched the pr5 value (must be cleared in software). 0 = timer5 time base did not match the pr5 value. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 99 pic18f2331/2431/4331/4431 9.3 pie registers the pie registers contain the individual enable bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are two peripheral interrupt enable registers (pie1, pie2). when ipen = 0 , the peie bit must be set to enable any of these peripheral interrupts. register 9-7: pie1: peripheral interrupt enable register 1 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie bit 7 bit 0 bit 7 unimplemented: read as ? 0 ? bit 6 adie: a/d converter interrupt enable bit 1 = enables the a/d interrupt 0 = disables the a/d interrupt bit 5 rcie: usart receive interrupt enable bit 1 = enables the usart receive interrupt 0 = disables the usart receive interrupt bit 4 txie: usart transmit interrupt enable bit 1 = enables the usart transmit interrupt 0 = disables the usart transmit interrupt bit 3 sspie: synchronous serial port interrupt enable bit 1 = enables the ssp interrupt 0 = disables the ssp interrupt bit 2 ccp1ie: ccp1 interrupt enable bit 1 = enables the ccp1 interrupt 0 = disables the ccp1 interrupt bit 1 tmr2ie: tmr2 to pr2 match interrupt enable bit 1 = enables the tmr2 to pr2 match interrupt 0 = disables the tmr2 to pr2 match interrupt bit 0 tmr1ie: tmr1 overflow interrupt enable bit 1 = enables the tmr1 overflow interrupt 0 = disables the tmr1 overflow interrupt legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 100 preliminary ? 2003 microchip technology inc. register 9-8: pie2: peripheral interrupt enable register 2 r/w-0 u-0 u-0 r/w-0 u-0 r/w-0 u-0 r/w-0 osfie ? ? eeie ?lvdie ? ccp2ie bit 7 bit 0 bit 7 osfie: oscillator fail interrupt enable bit 1 = enabled 0 =disabled bit 6-5 unimplemented: read as ? 0 ? bit 4 eeie: interrupt enable bit 1 = enabled 0 =disabled bit 3 unimplemented: read as ? 0 ? bit 2 lvdie: low-voltage detect interrupt enable bit 1 = enabled 0 =disabled bit 1 unimplemented: read as ? 0 ? bit 0 ccp2ie: ccp2 interrupt enable bit 1 = enabled 0 =disabled legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 101 pic18f2331/2431/4331/4431 register 9-9: pie3: peripheral interrupt enable register 3 u-0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? ? ptie ic3drie ic2qeie ic1ie tmr5ie bit 7 bit 0 bit 7-5 unimplemented: read as ? 0 ? bit 4 ptie: pwm time base interrupt enable bit 1 = ptif enabled 0 = ptif disabled bit 3 ic3drie: ic3 interrupt enable/direction change interrupt enable bit ic3 enabled (cap3con<3:0>) 1 = ic3 interrupt enabled 0 = ic3 interrupt disabled qei enabled (qeim<2:0>) 1 = change-of-direction interrupt enabled 0 = change-of-direction interrupt disabled bit 2 ic2qeie: ic2 interrupt flag/qei interrupt flag enable bit ic2 enabled (cap2con<3:0>) 1 = ic2 interrupt enabled) 0 = ic2 interrupt disabled qei enabled (qeim<2:0>) 1 = qei interrupt enabled 0 = qei interrupt disabled bit 1 ic1ie: ic1 interrupt enable bit 1 = ic1 interrupt enabled 0 = ic1 interrupt disabled bit 0 tmr5ie: timer5 interrupt enable bit 1 = timer5 interrupt enabled 0 = timer5 interrupt disabled legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 102 preliminary ? 2003 microchip technology inc. 9.4 ipr registers the ipr registers contain the individual priority bits for the peripheral interrupts. due to the number of peripheral interrupt sources, there are two peripheral interrupt priority registers (ipr1, ipr2). using the priority bits requires that the interrupt priority enable (ipen) bit be set. register 9-10: ipr1: peripheral interrupt priority register 1 u-0 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip bit 7 bit 0 bit 7 unimplemented: read as ? 0 ? bit 6 adip: a/d converter interrupt priority bit 1 =high priority 0 = low priority bit 5 rcip: usart receive interrupt priority bit 1 =high priority 0 = low priority bit 4 txip: usart transmit interrupt priority bit 1 =high priority 0 = low priority bit 3 sspip: synchronous serial port interrupt priority bit 1 =high priority 0 = low priority bit 2 ccp1ip: ccp1 interrupt priority bit 1 =high priority 0 = low priority bit 1 tmr2ip: tmr2 to pr2 match interrupt priority bit 1 =high priority 0 = low priority bit 0 tmr1ip: tmr1 overflow interrupt priority bit 1 =high priority 0 = low priority legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 103 pic18f2331/2431/4331/4431 register 9-11: ipr2: peripheral interrupt priority register 2 r/w-1 u-0 u-0 r/w-1 u-0 r/w-1 u-0 r/w-1 osfip ? ? eeip ?lvdip ? ccp2ip bit 7 bit 0 bit 7 osfip: oscillator fail interrupt priority bit 1 =high priority 0 = low priority bit 6-5 unimplemented: read as ? 0 ? bit 4 eeip: interrupt priority bit 1 =high priority 0 = low priority bit 3 unimplemented: read as ? 0 ? bit 2 lvdip: low-voltage detect interrupt priority bit 1 =high priority 0 = low priority bit 1 unimplemented: read as ? 0 ? bit 0 ccp2ip: ccp2 interrupt priority bit 1 =high priority 0 = low priority legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 104 preliminary ? 2003 microchip technology inc. register 9-12: ipr3: peripheral interrupt priority register 3 u-0 u-0 u-0 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 ? ? ptip ic3drip ic2qeip ic1ip tmr5ip bit 7 bit 0 bit 7-5 unimplemented: read as ? 0 ? bit 4 ptip: pwm time base interrupt priority bit 1 = high priority 0 = low priority bit 3 ic3drip: ic3 interrupt priority/direction change interrupt priority bit ic3 enabled (cap3con<3:0>) 1 = ic3 interrupt high priority 0 = ic3 interrupt low priority qei enabled (qeim<2:0>) 1 = change of direction interrupt high priority 0 = change of direction interrupt low priority bit 2 ic2qeip: ic2 interrupt priority/qei interrupt priority bit ic2 enabled (cap2con<3:0>) 1 = ic2 interrupt high priority 0 = ic2 interrupt low priority qei enabled (qeim<2:0>) 1 = high priority 0 = low priority bit 1 ic1ip: ic1 interrupt priority bit 1 = high priority 0 = low priority bit 0 tmr5ip: timer5 interrupt priority bit 1 = high priority 0 = low priority legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 105 pic18f2331/2431/4331/4431 9.5 rcon register the rcon register contains bits used to determine the cause of the last reset or wake-up from power- managed mode. rcon also contains the bit that enables interrupt priorities (ipen). register 9-13: rcon register r/w-0 u-0 u-0 r/w-1 r-1 r-1 r/w-0 r/w-0 ipen ? ?ri to pd por bor bit 7 bit 0 bit 7 ipen: interrupt priority enable bit 1 = enable priority levels on interrupts 0 = disable priority levels on interrupts (pic16cxxx compatibility mode) bit 6-5 unimplemented: read as ? 0 ? bit 4 ri : reset instruction flag bit for details of bit operation, see register 5-3 bit 3 to : watchdog time-out flag bit for details of bit operation, see register 5-3 bit 2 pd : power-down detection flag bit for details of bit operation, see register 5-3 bit 1 por : power-on reset status bit for details of bit operation, see register 5-3 bit 0 bor : brown-out reset status bit for details of bit operation, see register 5-3 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 106 preliminary ? 2003 microchip technology inc. 9.6 intn pin interrupts external interrupts on the rc3/int0, rc4/int1 and rc5/int2 pins are edge triggered: either rising, if the corresponding intedgx bit is set in the intcon2 register, or falling, if the intedgx bit is clear. when a valid edge appears on the rc3/int0 pin, the corresponding flag bit intxf is set. this interrupt can be disabled by clearing the corresponding enable bit intxe. flag bit intxf must be cleared in software in the interrupt service routine before re-enabling the interrupt. all external interrupts (int0, int1 and int2) can wake-up the processor from the power-managed modes, if bit intxe was set prior to going into power- managed modes. if the global interrupt enable bit gie is set, the processor will branch to the interrupt vector following wake-up. interrupt priority for int1 and int2 is determined by the value contained in the interrupt priority bits, int1ip (intcon3<6>) and int2ip (intcon3<7>). there is no priority bit associated with int0. it is always a high priority interrupt source. 9.7 tmr0 interrupt in 8-bit mode (which is the default), an overflow (ffh 00h) in the tmr0 register will set flag bit tmr0if. in 16-bit mode, an overflow (ffffh 0000h) in the tmr0h:tmr0l registers will set flag bit tmr0if. the interrupt can be enabled/disabled by setting/clear- ing enable bit tmr0ie (intcon<5>). interrupt priority for timer0 is determined by the value contained in the interrupt priority bit tmr0ip (intcon2<2>). see section 11.0 ?timer0 module? for further details. 9.8 portb interrupt-on-change an input change on portb<7:4> sets flag bit rbif (intcon<0>). the interrupt can be enabled/disabled by setting/clearing enable bit, rbie (intcon<3>). interrupt priority for portb interrupt-on-change is determined by the value contained in the interrupt priority bit, rbip (intcon2<0>). 9.9 context saving during interrupts during interrupts, the return pc address is saved on the stack. additionally, the wreg, status and bsr registers are saved on the fast return stack. if a fast return from interrupt is not used (see section 5.3 ?fast register stack? ), the user may need to save the wreg, status and bsr registers on entry to the interrupt service routine. depending on the user?s application, other registers may also need to be saved. example 9-1 saves and restores the wreg, status and bsr registers during an interrupt service routine. example 9-1: saving status, wreg and bsr registers in ram movwf w_temp ; w_temp is in virtual bank movff status,status_temp ; status_temp located anywhere movff bsr,bsr_temp ; bsr_tmep located anywhere ; ; user isr code ; movff bsr_temp,bsr ; restore bsr movf w_temp, w ; restore wreg movff status_temp, status ; restore status
? 2003 microchip technology inc. preliminary ds39616b-page 107 pic18f2331/2431/4331/4431 10.0 i/o ports depending on the device selected and features enabled, there are up to five ports available. some pins of the i/o ports are multiplexed with an alternate function from the peripheral features on the device. in general, when a peripheral is enabled, that pin may not be used as a general purpose i/o pin. each port has three registers for its operation. these registers are:  tris register (data direction register)  port register (reads the levels on the pins of the device)  lat register (output latch) the data latch (lat register) is useful for read-modify- write operations on the value that the i/o pins are driving. a simplified model of a generic i/o port without the interfaces to other peripherals is shown in figure 10-1. figure 10-1: generic i/o port operation 10.1 porta, trisa and lata registers porta is a 8-bit wide, bidirectional port. the corre- sponding data direction register is trisa. setting a trisa bit (= 1 ) will make the corresponding porta pin an input (i.e., put the corresponding output driver in a high-impedance mode). clearing a trisa bit (= 0 ) will make the corresponding porta pin an output (i.e., put the contents of the output latch on the selected pin). reading the porta register reads the status of the pins, whereas writing to it, will write to the port latch. the data latch register (lata) is also memory mapped. read-modify-write operations on the lata register read and write the latched output value for porta. the ra<2:4> pins are multiplexed with three input capture pins and quadrature encoder interface pins. pins ra6 and ra7 are multiplexed with the main oscillator pins; they are enabled as oscillator or i/o pins by the selection of the main oscillator in configuration register 1h (see section 22.1 ?configuration bits? for details). when they are not used as port pins, ra6 and ra7 and their associated tris and lat bits are read as ? 0 ?. the other porta pins are multiplexed with analog inputs, the analog v ref + and v ref - inputs and the com- parator voltage reference output. the operation of pins ra3:ra0 and ra5 as a/d converter inputs is selected by clearing/setting the control bits in the ansel0 and ansel1 registers. the trisa register controls the direction of the ra pins, even when they are being used as analog inputs. the user must ensure the bits in the trisa register are maintained set when using them as analog inputs. example 10-1: initializing porta data bus wr lat wr tris rd port data latch tris latch rd tris input buffer i/o pin (1) q d ck q d ck en qd en rd lat or port note 1: i/o pins have diode protection to v dd and v ss . note 1: on a power-on reset, ra5:ra0 are con- figured as analog inputs and read as ? 0 ?. 2: ra5 i/f is available only on 40-pin devices (pic18f4x31). clrf porta ; initialize porta by ; clearing output ; data latches clrf lata ; alternate method ; to clear output ; data latches movlw 0x3f ; configure a/d movwf ansel0 ; for digital inputs movlw 0xcf ; value used to ; initialize data ; direction movwf trisa ; set ra<3:0> as inputs ; ra<5:4> as outputs
pic18f2331/2431/4331/4431 ds39616b-page 108 preliminary ? 2003 microchip technology inc. figure 10-2: block diagram of ra0 figure 10-3: block diagram of ra1 figure 10-4: block diag ram of ra3:ra2 pins data bus q d q ck q d q ck qd en p n wr lata wr trisa data latch tris latch rd trisa rd porta v ss v dd i/o pin analog input mode ttl input buffer to a/d converter rd lata or porta data bus wr lata wr trisa data latch tris latch rd trisa q d q ck qd en q d q ck p n v dd v ss rd porta ra1 or porta rd lata analog input mode to a/d converter ttl data bus wr lata wr trisa data latch tris latch rd trisa q d q ck qd en q d q ck p n v dd v ss rd porta i/o pin or porta rd lata analog input mode to a/d converter ttl schmitt trigger input buffer to cap1/indx or cap2/qea
? 2003 microchip technology inc. preliminary ds39616b-page 109 pic18f2331/2431/4331/4431 figure 10-5: block diagram of ra4 data bus q d q ck q d q ck qd en wr lata wr trisa data latch tris latch rd trisa rd porta ra4 (1) note 1: open-drain usually available on ra 4 has been removed for this device. analog input mode ttl input buffer to a/d converter rd lata or porta to cap3/qeb p n v ss v dd schmitt trigger input buffer
pic18f2331/2431/4331/4431 ds39616b-page 110 preliminary ? 2003 microchip technology inc. figure 10-6: block diagram of ra5 figure 10-7: block diagram of ra6 figure 10-8: block diagram of ra7 data bus q d q ck q d q ck qd en p n wr lata wr trisa data latch tris latch rd trisa rd porta v ss v dd i/o analog input mode or ttl input buffer to a/d converter/lvd module input or porta rd lata lvdin enabled pin data bus q d q ck q d q ck qd en p n wr lata data latch tris latch rd trisa rd porta v ss v dd i/o or porta rd lata ecra6 or rcra6 ecra6 or enable ttl input buffer rcra6 t o o scillator enable pin data bus q d q ck q d q ck qd en p n wr lata data latch tris latch rd trisa rd porta v ss v dd i/o or porta rd lata intosc enable ttl input buffer intosc t o o scillator pin w/ra7 enable
? 2003 microchip technology inc. preliminary ds39616b-page 111 pic18f2331/2431/4331/4431 table 10-1: porta functions table 10-2: summary of registers associated with porta name bit # buffer function ra0/an0 bit 0 ttl input/output or analog input. ra1/an1 bit 1 ttl input/output or analog input. ra2/an2/v ref -/cap1/indx bit 2 ttl/st input/output, analog input, v ref -, capture input, or qei index input. ra3/an3/v ref +/cap2/qea bit 3 ttl/st input/output, analog input, v ref +, capture input, or quadrature channel a input. ra4/an4/cap3/qeb bit 4 ttl/st input/output, analog input, capture input, or quadrature channel b input. ra5/an5/lvdin bit 5 ttl input/output, analog input, or low-voltage detect input. osc2/clko/ra6 bit 6 ttl osc2, clock output or i/o pin. osc1/clki/ra7 bit 7 ttl osc1, clock input or i/o pin. legend: ttl = ttl input, st = schmitt trigger input name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets porta ra7 (1) ra6 (1) ra5 ra4 ra3 ra2 ra1 ra0 xx0x 0000 uu0u 0000 lata lata7 (1) lata6 (1) lata data output register xxxx xxxx uuuu uuuu trisa trisa7 (1) trisa6 (1) porta data direction register 1111 1111 1111 1111 adcon1 vcfg1 vcfg0 ? fifoen bfemt bfovfl adpnt1 adpnt0 00-1 0000 00-1 0000 ansel0 ans7 (2) ans6 (2) ans5 (2) ans4 ans3 ans2 ans1 ans0 1111 1111 1111 1111 ansel1 ? ? ? ? ? ? ?ans8 (2) ---- ---1 ---- ---1 legend: x = unknown, u = unchanged, ? = unimplemented locations read as ? 0 ?. shaded cells are not used by porta. note 1: ra7:ra6 and their associated latch and data directio n bits are enabled as i/o pins based on oscillator configuration; otherwise, they are read as ? 0 ?. 2: ans5 through ans8 are availabl e only on the pic18f4x31 devices.
pic18f2331/2431/4331/4431 ds39616b-page 112 preliminary ? 2003 microchip technology inc. 10.2 portb, trisb and latb registers portb is an 8-bit wide, bidirectional port. the corre- sponding data direction register is trisb. setting a trisb bit (= 1 ) will make the corresponding portb pin an input (i.e., put the corresponding output driver in a high-impedance mode). clearing a trisb bit (= 0 ) will make the corresponding portb pin an output (i.e., put the contents of the output latch on the selected pin). the data latch register (latb) is also memory mapped. read-modify-write operations on the latb register read and write the latched output value for portb. example 10-2: initializing portb each of the portb pins has a weak internal pull-up. a single control bit can turn on all the pull-ups. this is performed by clearing bit rbpu (intcon2<7>). the weak pull-up is automatically turned off when the port pin is configured as an output. the pull-ups are disabled on a power-on reset. four of the portb pins (rb7:rb4) have an interrupt- on-change feature. only pins configured as inputs can cause this interrupt to occur (i.e., any rb7:rb4 pin configured as an output is excluded from the interrupt- on-change comparison). the input pins (of rb7:rb4) are compared with the old value latched on the last read of portb. the ?mismatch? outputs of rb7:rb4 are ored together to generate the rb port change interrupt with flag bit, rbif (intcon<0>). this interrupt can wake the device from sleep. the user, in the interrupt service routine, can clear the interrupt in the following manner: a) any read or write of portb (except with the movff (any), portb instruction). this will end the mismatch condition. b) clear flag bit rbif. a mismatch condition will continue to set flag bit rbif. reading portb will end the mismatch condition and allow flag bit rbif to be cleared. the interrupt-on-change feature is recommended for wake-up on key depression operation and operations where portb is only used for the interrupt-on-change feature. polling of portb is not recommended while using the interrupt-on-change feature. rb<0:3> and rb4 pins are multiplexed with the 14-bit pwm module for pwm<0:3> and pwm5 output. the rb5 pin can be configured by the configuration bit pwm4mx as the alternate pin for pwm4 output. clrf portb ; initialize portb by ; clearing output ; data latches clrf latb ; alternate method ; to clear output ; data latches movlw 0xcf ; value used to ; initialize data ; direction movwf trisb ; set rb<3:0> as inputs ; rb<5:4> as outputs ; rb<7:6> as inputs
? 2003 microchip technology inc. preliminary ds39616b-page 113 pic18f2331/2431/4331/4431 figure 10-9: block diag ram of rb3:rb0 pins port/pwm select data bus wr latb wr trisb data latch tris latch rd trisb q d q ck qd en pwm0,1,2, 3 data 0 1 q d q ck p n v dd v ss rd portb rb<3:0> or portb rd latc note 1: to enable weak pull-ups, set the appropriate tris bit(s) and clear the rbpu bit (intcon2<7>). rbpu (1) v dd p weak pull-up ttl input buffer pins
pic18f2331/2431/4331/4431 ds39616b-page 114 preliminary ? 2003 microchip technology inc. figure 10-10: block diagram of rb4 port/pwm select data bus wr latb wr trisb data latch tris latch rd trisb q d q ck qd en pwm5 data 0 1 q d q ck p n v dd v ss rd portb rb4 pin or portb rd latc note 1: to enable weak pull-ups, set the appropriate tris bit(s) and clear the rbpu bit (intcon2<7>). rbpu (1) v dd p weak pull-up from other qd en set rbif rb7:rb4 pins rd portb q3 rd latb q1 ttl input buffer
? 2003 microchip technology inc. preliminary ds39616b-page 115 pic18f2331/2431/4331/4431 figure 10-11: block diagram of rb5 data bus wr port wr tris rd port data latch tris latch rb5/pgm q d ck q d ck rd tris rbpu p v dd weak pull-up from other qd en qd en set rbif rb7:rb4 pins rd port q3 q1 ttl input buffer lvp configuration bit 1 = low v prog enable 0 = only hv prog enable icsp p n v dd v ss 0 1 port/pwm select pwm4 data q schmitt trigger
pic18f2331/2431/4331/4431 ds39616b-page 116 preliminary ? 2003 microchip technology inc. figure 10-12: block diagram of rb7:rb6 pins data bus wr latb wr trisb data latch tris latch rd trisc q d q ck qd en q d q ck rd portb rb7/rb6 or portb rd latb schmitt trigger rbpu (1) p weak pull-up q1 from other qd en set rbif rb7:rb4 pins rd portb q3 pgc (2) /pgd (3) note 1: to enable weak pull-ups, set the appropriate tris bit(s) and clear the rbpu bit (intcon2<7>). 2: pgc is available on rb6. 3: pgd is available on rb7. ttl input buffer enable debug or icsp 0 1 debug 01 enable enable debug or icsp brbx btrisx enable debug pin
? 2003 microchip technology inc. preliminary ds39616b-page 117 pic18f2331/2431/4331/4431 table 10-3: portb functions table 10-4: summary of registers associated with portb name bit # buffer function rb0/pwm0 bit 0 ttl (1) input/output pin, or pcpwm output pwm0. internal software programmable weak pull-up. rb1/pwm1 bit 1 ttl (1) input/output pin, or pcpwm output pwm1. internal software programmable weak pull-up. rb2/pwm2 bit 2 ttl (1) input/output pin, or pcpwm output pwm2. internal software programmable weak pull-up. rb3/pwm3 bit 3 ttl (1) input/output pin, or pcpwm output pwm3. internal software programmable weak pull-up. rb4/kbi0/pwm5 bit 4 ttl input/output pin (with interrupt-on-change), or pcpwm output pwm5. internal software programmable weak pull-up. rb5/kbi1/pwm4/ pgm bit 5 ttl/st (2) input/output pin (with interrupt-on-change) or pcpwm output pwm4. internal software programmable weak pull-up. low-voltage icsp enable pin. rb6/kbi2/pgc bit 6 ttl/st (2) input/output pin (with interrupt-on-change). internal software programmable weak pull-up. serial programming clock. rb7/kbi3/pgd bit 7 ttl/st (2) input/output pin (with interrupt-on-change). internal software programmable weak pull-up. serial programming data. legend: ttl = ttl input, st = schmitt trigger input note 1: this buffer is a ttl input when configured as digital i/o. 2: this buffer is a schmitt trigger input when used in serial programming mode. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets portb rb7 rb6 rb5 rb4 rb3 rb2 rb1 rb0 xxxq qqqq uuuu uuuu latb latb data output register xxxx xxxx uuuu uuuu trisb portb data direction register 1111 1111 1111 1111 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u intcon2 rbpu intedg0 intedg1 intedg2 ? tmr0ip ?rbip 1111 -1-1 1111 -1-1 intcon3 int2ip int1ip ? int2ie int1ie ? int2if int1if 11-0 0-00 11-0 0-00 legend: x = unknown, u = unchanged, q = value depends on condition. shaded cells are not used by portb.
pic18f2331/2431/4331/4431 ds39616b-page 118 preliminary ? 2003 microchip technology inc. 10.3 portc, trisc and latc registers portc is an 8-bit wide, bidirectional port. the corre- sponding data direction register is trisc. setting a trisc bit (= 1 ) will make the corresponding portc pin an input (i.e., put the corresponding output driver in a high-impedance mode). clearing a trisc bit (= 0 ) will make the corresponding portc pin an output (i.e., put the contents of the output latch on the selected pin). the data latch register (latc) is also memory mapped. read-modify-write operations on the latc register read and write the latched output value for portc. portc is multiplexed with several peripheral functions (table 10-5). the pins have schmitt trigger input buffers. when enabling peripheral functions, care should be taken in defining tris bits for each portc pin. some peripherals override the tris bit to make a pin an output, while other peripherals override the tris bit to make a pin an input. the user should refer to the corresponding peripheral section for the correct tris bit settings. the contents of the trisc register are affected by peripheral overrides. reading trisc always returns the current contents, even though a peripheral device may be overriding one or more of the pins. external interrupts, in0, int1 and int2, are placed on rc3, rc4 and rc5 respectively. ssp alternate interface pins, sdi/sda, sck/scl and sdo are placed on rc4, rc5, and rc7 pins respec- tively. these pins are multiplexed on portc and portd by using the sspmx bit in the config3l register. usart pins rx/dt and tx/ck are placed on rc7 and rc6 respectively. the alternate timer5 external clock input, t5cki, and the alternate tmr0 external clock input, t0cki, are placed on rc3 and are multiplexed with the portd (rd0) pin using the exclkmx configuration bit in config3l. fault inputs to the 14-bit pwm module, flta and fltb , are located on rc1 and rc2. flta input on rc1 is multiplexed with rd4 using the fltamx bit. example 10-3: initializing portc figure 10-13: block diagram of rc0 note: on a power-on reset, these pins are configured as digital inputs. clrf portc ; initialize portc by ; clearing output ; data latches clrf latc ; alternate method ; to clear output ; data latches movlw 0xcf ; value used to ; initialize data ; direction movwf trisc ; set rc<3:0> as inputs ; rc<5:4> as outputs ; rc<7:6> as inputs data bus wr latc wr trisc data latch tris latch rd trisc schmitt trigger q d q ck qd en q d q ck p n v dd v ss rd portc t1 osc en t1 clock input rc0 pin or portc rd latc to rc1 pin timer1 oscillator
? 2003 microchip technology inc. preliminary ds39616b-page 119 pic18f2331/2431/4331/4431 figure 10-14: block diagram of rc1 figure 10-15: block diagram of rc2 port/ccp2 select data bus wr latc wr trisc data latch tris latch rd trisc q d q ck qd en ccp2 data out 0 1 q d q ck p n v dd v ss rd portc ccp2 input rc1 pin or portc rd latc schmitt trigger to r c 0 p i n fltamx flta input (1) note 1: flta input is multiplexed with rc1 and rd4 using fltamx configuration bit in config3l register. port/ccp1 select data bus wr latc wr trisc data latch tris latch rd trisc q d q ck qd en ccp1 data out 0 1 q d q ck p n v dd v ss rd portc ccp1 input/fltb input rc2 pin or portc rd latc schmitt trigger
pic18f2331/2431/4331/4431 ds39616b-page 120 preliminary ? 2003 microchip technology inc. figure 10-16: block diagram of rc3 figure 10-17: block diagram of rc4 data bus wr latc wr trisc data latch tris latch rd trisc q d q ck qd en q d q ck p n v dd v ss rd portc t0cki/t5cki input rc3 pin or portc rd latc schmitt trigger note 1: the t0cki/t5cki bit is multiplexed with rd0 when the exclkm is enabled (= 1 ) in the configuration register. exclkmx_enable (1) port/ssp mode & sspmx select data bus wr latc wr trisc data latch tris latch rd trisc q d q ck qd en sda data out 0 1 q d q ck p n v dd v ss rd portc sda drive sdi/sda input rc4 pin or portc rd latc schmitt trigger note 1: the sdi/sda bits are multiplexed on rd2 and rc4 pi ns by sspmx bit in the configuration register. sspmx (1)
? 2003 microchip technology inc. preliminary ds39616b-page 121 pic18f2331/2431/4331/4431 figure 10-18: block diagram of rc5 figure 10-19: block diagram of rc6 port/ sspen & sspmx_ select data bus wr latc wr trisc data latch tris latch rd trisc q d q ck qd en sck/scl data out 0 1 q d q ck p n v dd v ss rd portc sdo drive rc5 pin or portc rd latc schmitt trigger note 1: sck/scl are multiplexed on rd3 and rc5 using sspmx bit in the configuration register. scl or scl input sspmx (1) i 2 c? mode usart select data bus wr latc wr trisc data latch tris latch rd trisc q d q ck qd en tx data out/ck 0 1 q d q ck p n v dd v ss rd portc usart select ss input rc6 pin or portc rd latc schmitt trigger ck input ttl
pic18f2331/2431/4331/4431 ds39616b-page 122 preliminary ? 2003 microchip technology inc. figure 10-20: block diagram of rc6 port/(sspen * spi mode ) select data bus wr latc wr trisc data latch tris latch rd trisc q d q ck qd en sdo data out (2) 0 1 q d q ck p n v dd v ss rd portc rc7 pin or portc rd latc schmitt trigger note 1: usart is in synchronous master transmission mode only (sync = 1 , txen = 1 ). 2: sdo must have its trisc bit cleared in order to be able to drive rc7. rx/dt data input usart select (1) 0 1 dt data out usart select (1)
? 2003 microchip technology inc. preliminary ds39616b-page 123 pic18f2331/2431/4331/4431 table 10-5: portc functions table 10-6: summary of registers associated with portc name bit # buffer type function rc0/t1oso/t1cki bit 0 st input/output port pin or timer1 oscillator output/timer1 clock input. rc1/t1osi/ccp2/ flta bit 1 st/cmos input/output port pin, timer1 oscillator input, or capture2 input/ compare2 output/pwm output when ccp2mx configuration bit is disabled, or flta input. rc2/ccp1/fltb bit 2 st input/output port pin, capture1 input/compare1 output/pwm1 output, or fltb input. rc3/t0cki/t5cki/ int0 bit 3 st input/output port pin, timer0 and timer5 alternate clock input, or external interrupt 0. rc4/int1/sdi/sda bit 4 st input/output port pin, spi data in, i 2 c data i/o, or external interrupt 1. rc5/int2/sck/scl bit 5 st input/output port pin or synchronous serial port clock i/o, or external interrupt 2. rc6/tx/ck/ss bit 6 st input/output port pin, eusart asynchronous transmit, eusart synchronous clock, or spi slave select input. rc7/rx/dt/sdo bit 7 st input/output port pin, eusart asynchronous receive, eusart synchronous data, or spi data out. legend: st = schmitt trigger input name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets portc rc7 rc6 rc5 rc4 rc3 rc2 rc1 rc0 xxxx xxxx uuuu uuuu latc latc data output register xxxx xxxx uuuu uuuu trisc portc data direction register 1111 1111 1111 1111 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u intcon2 rbpu intedg0 intedg1 intedg2 ? tmr0ip ? rbip 1111 -1-1 1111 -1-1 intcon3 int2ip int1ip ? int2ie int1ie ? int2if int1if 11-0 0-00 11-0 0-00 legend: x = unknown, u = unchanged
pic18f2331/2431/4331/4431 ds39616b-page 124 preliminary ? 2003 microchip technology inc. 10.4 portd, trisd and latd registers portd is an 8-bit wide, bidirectional port. the corresponding data direction register is trisd. setting a trisd bit (= 1 ) will make the corresponding portd pin an input (i.e., put the corresponding output driver in a high-impedance mode). clearing a trisd bit (= 0 ) will make the corresponding portd pin an output (i.e., put the contents of the output latch on the selected pin). the data latch register (latd) is also memory mapped. read-modify-write operations on the latd register read and write the latched output value for portd. all pins on portd are implemented with schmitt trigger input buffers. each pin is individually configurable as an input or output. portd includes pwm<7:6> complementary fourth channel pwm outputs. pwm4 is the complementary output of pwm5 (the third channel), which is multi- plexed with the rb5 pin. this output can be used as the alternate output using the pwm4mx configuration bit in config3l when the low-voltage programming pin (pgm) is used on rb5. rd1, rd2 and rd3 can be used as the alternate out- put for sdo, sdi/sda and sck/scl using the sspmx configuration bit in config3l. rd4 an be used as the alternate output for flta using the fltamx configuration bit in config3l. example 10-4: initializing portd figure 10-21: block diagram of rd7:rd6 pins note: portd is only available on pic18f4x31 devices. note: on a power-on reset, these pins are configured as digital inputs. clrf portd ; initialize portd by ; clearing output ; data latches clrf latd ; alternate method ; to clear output ; data latches movlw 0xcf ; value used to ; initialize data ; direction movwf trisd ; set rd<3:0> as inputs ; rd<5:4> as outputs ; rd<7:6> as inputs data bus wr latd wr trisd data latch tris latch rd trisd q d q ck qd en q d q ck p n v dd v ss rd portd rd[7:6] pin or portd rd latd schmitt trigger port/pwm select 0 1 pwm6,7 data out
? 2003 microchip technology inc. preliminary ds39616b-page 125 pic18f2331/2431/4331/4431 figure 10-22: block diagram of rd5 figure 10-23: block diagram of rd4 data bus wr latd wr trisd data latch tris latch rd trisd q d q ck q d en q d q ck p n v dd v ss rd portd rd5 pin or portd rd latd schmitt trigger port/pwm select 0 1 pwm4 data out * data bus wr latd wr trisd data latch tris latch rd trisd q d q ck qd en q d q ck p n v dd v ss rd portd rd4 pin or portd rd latd schmitt trigger note 1: fltamx is located in the configuration register. schmitt trigger flta input fltamx (1)
pic18f2331/2431/4331/4431 ds39616b-page 126 preliminary ? 2003 microchip technology inc. figure 10-24: block diagram of rd3 figure 10-25: block diagram of rd2 port/ sspen & sspmx select data bus wr latd wr trisd data latch tris latch rd trisd q d q ck qd en sck/scl data out 0 1 q d q ck p n v dd v ss rd portc rd3 pin or portd rd latd schmitt trigger note 1: sck/scl are multiplexed on rd3 and rc5 using sspmx bit in the configuration register. sck or scl input sspmx (1) i 2 c? mode port/ssp mode & sspmx select data bus wr latc wr trisc data latch tris latch rd trisc q d q ck qd en sda data out 0 1 q d q ck p n v dd v ss rd portc sda drive sdi/sda input rd2pin or portc rd latc schmitt trigger note 1: the sdi/sda bits are multiplexed on rd2 and rc4 pins by sspmx bit in the configuration register. sspmx (1)
? 2003 microchip technology inc. preliminary ds39616b-page 127 pic18f2331/2431/4331/4431 figure 10-26: block diagram of rd1 figure 10-27: block diagram of rd0 port/spi mode & sspmx select data bus wr latd wr trisd data latch tris latch rd trisd q d q ck qd en sdo data out 0 1 q d q ck p n v dd v ss rd portd rd1 pin or portd rd latd schmitt trigger note 1: the sdo output is multiplexed by sspmx bit in the configuration register. data bus wr latd wr trisd data latch tris latch rd trisd q d q ck qd en q d q ck p n v dd v ss rd portd t0cki/t5cki input rd0 pin or portd rd latd schmitt trigger note 1: t0cki/t5cki are multiplexed by sspmx bit in the configuration register. sspmx (1)
pic18f2331/2431/4331/4431 ds39616b-page 128 preliminary ? 2003 microchip technology inc. table 10-7: portd functions table 10-8: summary of registers associated with portd name bit # buffer type function rd0/t0cki/t5cki bit 0 st input/output port pin. rd1/sdo bit 1 st input/output port pin. rd2/sdi/sda bit 2 st input/output port pin. rd3/sck/scl bit 3 st input/output port pin. rd4/flta bit 4 st input/output port pin. rd5/pwm4 bit 5 st input/output port pin, or pcpwm output pwm4. rd6/pwm6 bit 6 st input/output port pin, or pcpwm output pwm6. rd7/pwm7 bit 7 st input/output port pin, or pcpwm output pwm7. legend: st = schmitt trigger input, ttl = ttl input name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets portd rd7 rd6 rd5 rd4 rd3 rd2 rd1 rd0 xxxx xxxx uuuu uuuu latd latd data output register xxxx xxxx uuuu uuuu trisd portd data direction register 1111 1111 1111 1111 legend: x = unknown, u = unchanged, ? = unimplemented, read as ? 0 ?. shaded cells are not used by portd.
? 2003 microchip technology inc. preliminary ds39616b-page 129 pic18f2331/2431/4331/4431 10.5 porte, trise and late registers porte is a 4-bit wide bidirectional port. three pins (re0/an6, re1/an67 and re2/an8) are individually configurable as inputs or outputs. these pins have schmitt trigger input buffers. when selected as an analog input, these pins will read as ? 0 ?s. the corresponding data direction register is trise. setting a trise bit (= 1 ) will make the corresponding porte pin an input (i.e., put the corresponding output driver in a high-impedance mode). clearing a trise bit (= 0 ) will make the corresponding porte pin an output (i.e., put the contents of the output latch on the selected pin). trise controls the direction of the re pins, even when they are being used as analog inputs. the user must make sure to keep the pins configured as inputs when using them as analog inputs. the data latch register (late) is also memory mapped. read-modify-write operations on the late register read and write the latched output value for porte. the fourth pin of porte (m clr /v pp /re3) is an input only pin. its operation is controlled by the mclre con- figuration bit in configuration register 3h (config3h<7>). when selected as a port pin (mclre = 0 ), it functions as a digital input only pin. as such, it does not have tris or lat bits associated with its operation. otherwise, it functions as the device?s master clear input. in either configuration, re3 also functions as the programming voltage input during programming. example 10-5: initializing porte 10.5.1 porte in 28-pin devices for pic18f2x31 devices, porte is only available when master clear functionality is disabled (config3h<7> = 0 ). in these cases, porte is a single bit, input only port comprised of re3 only. the pin operates as previously described. note: porte is only available on pic18f4x31 devices. note: on a power-on reset, re2:re0 are configured as analog inputs. note: on a power-on reset, re3 is enabled as a digital input only if master clear functionality is disabled. clrf porte ; initialize porte by ; clearing output ; data latches clrf late ; alternate method ; to clear output ; data latches movlw 0x3f ; configure a/d movwf ansel0 ; for digital inputs bcf ansel1, 0 ; movlw 0x03 ; value used to ; initialize data ; direction movwf trise ; set re<0> as input ; re<1> as output ; re<2> as input
pic18f2331/2431/4331/4431 ds39616b-page 130 preliminary ? 2003 microchip technology inc. figure 10-28: re2:re0 block diagram figure 10-29: re3 block diagram data bus wr late wr trise data latch tris latch rd trise q d q ck qd en q d q ck p n v dd v ss rd porte re<0:2> or porte rd late schmitt trigger ttl to a/d converter ch. an6 or an7 or an8 analog input mode pins mclr /v pp /re3 data bus rd porte rd late schmitt trigger mclre rd trise qd en latch filter hv low level mclr detect high voltage detect internal mclr mclre note 1: pin requires special protection due to hv.
? 2003 microchip technology inc. preliminary ds39616b-page 131 pic18f2331/2431/4331/4431 register 10-1: trise register u-0 u-0 u-0 u-0 u-0 r/w-1 r/w-1 r/w-1 ? ? ? ? ? trise2 trise1 trise0 bit 7 bit 0 bit 7 unimplemented: read as ? 0 ? bit 6 unimplemented: read as ? 0 ? bit 5 unimplemented: read as ? 0 ? bit 4 unimplemented: read as ? 0 ? bit 3 unimplemented: read as ? 0 ? bit 2 trise2 : re2 direction control bit 1 = input 0 = output bit 1 trise1 : re1 direction control bit 1 = input 0 = output bit 0 trise0 : re0 direction control bit 1 = input 0 = output legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 132 preliminary ? 2003 microchip technology inc. table 10-9: porte functions table 10-10: summary of registers associated with porte name bit # buffer type function re0/an6 bit 0 st input/output port pin, analog input. re1/an7 bit 1 st input/output port pin, analog input. re2/an8 bit 2 st input/output port pin, analog input. mclr /v pp /re3 bit 3 st input only port pin or programming voltage input (if mclr is disabled); master clear input or programming voltage input (if mclr is enabled). legend: st = schmitt trigger input, ttl = ttl input name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets porte ? ? ? ?re3 (1) re2 re1 re0 ---- q000 ---- q000 late ? ? ? ? ? late data output register ---- -xxx ---- -uuu trise ? ? ? ? ? porte data direction bits ---- -111 ---- -111 ansel0 ans7 ans6 ans5 ans4 ans3 ans2 ans1 ans0 1111 1111 1111 1111 ansel1 ans15 ans14 ans13 ans12 ans11 ans10 ans9 ans8 ---- ---0 ---- ---0 legend: x = unknown, u = unchanged, ? = unimplemented, read as ? 0 ?, q = value depends on condition. shaded cells are not used by porte. note 1: implemented only when master clear functionality is disabled (config3h<7> = 0 ).
? 2003 microchip technology inc. preliminary ds39616b-page 133 pic18f2331/2431/4331/4431 11.0 timer0 module the timer0 module has the following features:  software selectable as an 8-bit or 16-bit timer/ counter  readable and writable  dedicated 8-bit software programmable prescaler  clock source selectable to be external or internal  interrupt-on-overflow from ffh to 00h in 8-bit mode and ffffh to 0000h in 16-bit mode  edge select for external clock figure 11-1 shows a simplified block diagram of the timer0 module in 8-bit mode and figure 11-2 shows a simplified block diagram of the timer0 module in 16-bit mode. the t0con register (register 11-1) is a readable and writable register that controls all the aspects of timer0, including the prescale selection. register 11-1: t0con: timer0 control register r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 tmr0on t016bit t0cs t0se psa t0ps2 t0ps1 t0ps0 bit 7 bit 0 bit 7 tmr0on: timer0 on/off control bit 1 = enables timer0 0 = stops timer0 bit 6 t016bit: timer0 16-bit control bit 1 = timer0 is configured as an 8-bit timer/counter 0 = timer0 is configured as a 16-bit timer/counter bit 5 t0cs: timer0 clock source select bit 1 = transition on t0cki pin 0 = internal instruction cycle clock (clko) bit 4 t0se: timer0 source edge select bit 1 = increment on high-to-low transition on t0cki pin 0 = increment on low-to-high transition on t0cki pin bit 3 psa: timer0 prescaler assignment bit 1 = timer0 prescaler is not assigned. timer0 clock input bypasses prescaler. 0 = timer0 prescaler is assigned. timer0 clock input comes from prescaler output. bit 2-0 t0ps2:t0ps0: timer0 prescaler select bits 111 =1:256 prescale value 110 =1:128 prescale value 101 =1:64 prescale value 100 =1:32 prescale value 011 =1:16 prescale value 010 =1:8 prescale value 001 =1:4 prescale value 000 =1:2 prescale value legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 134 preliminary ? 2003 microchip technology inc. figure 11-1: timer0 block diagram in 8-bit mode figure 11-2: timer0 block diagram in 16-bit mode note: upon reset, timer0 is enabled in 8-bit mode with clock input from t0cki maximum prescale. t0cki pin t0se 0 1 0 1 t0cs f osc /4 programmable prescaler sync with internal clocks tmr0 (2 t cy delay) data bus 8 psa t0ps2, t0ps1, t0ps0 set interrupt flag bit tmr0if on overflow 3 note: upon reset, timer0 is enabled in 8-bit mode with clock input from t0cki maximum prescale. t0se 0 1 0 1 t0cs f osc /4 programmable prescaler sync with internal clocks tmr0l (2 t cy delay) data bus<7:0> 8 psa t0ps2, t0ps1, t0ps0 set interrupt flag bit tmr0if on overflow 3 tmr0 tmr0h high byte 8 8 8 read tmr0l write tmr0l t0cki pin
? 2003 microchip technology inc. preliminary ds39616b-page 135 pic18f2331/2431/4331/4431 11.1 timer0 operation timer0 can operate as a timer or as a counter. timer mode is selected by clearing the t0cs bit. in timer mode, the timer0 module will increment every instruction cycle (without prescaler). if the tmr0 regis- ter is written, the increment is inhibited for the following two instruction cycles. the user can work around this by writing an adjusted value to the tmr0 register. counter mode is selected by setting the t0cs bit. in counter mode, timer0 will increment, either on every rising or falling edge of pin rc3/t0cki. the increment- ing edge is determined by the timer0 source edge select bit (t0se). clearing the t0se bit selects the rising edge. when an external clock input is used for timer0, it must meet certain requirements. the requirements ensure the external clock can be synchronized with the internal phase clock (t osc ). also, there is a delay in the actual incrementing of timer0 after synchronization. 11.2 prescaler an 8-bit counter is available as a prescaler for the timer0 module. the prescaler is not readable or writable. the psa and t0ps2:t0ps0 bits determine the prescaler assignment and prescale ratio. clearing bit psa will assign the prescaler to the timer0 module. when the prescaler is assigned to the timer0 module, prescale values of 1:2, 1:4, ..., 1:256 are selectable. when assigned to the timer0 module, all instructions writing to the tmr0 register (e.g., clrf tmr0, movwf tmr0, bsf tmr0, x ....etc.) will clear the prescaler count. 11.2.1 switching prescaler assignment the prescaler assignment is fully under software con- trol (i.e., it can be changed ?on-the-fly? during program execution). 11.3 timer0 interrupt the tmr0 interrupt is generated when the tmr0 register overflows from ffh to 00h in 8-bit mode, or ffffh to 0000h in 16-bit mode. this overflow sets the tmr0if bit. the interrupt can be masked by clearing the tmr0ie bit. the tmr0if bit must be cleared in software by the timer0 module interrupt service routine before re-enabling this interrupt. the tmr0 interrupt cannot awaken the processor from sleep mode, since the timer requires clock cycles, even when t0cs is set. 11.4 16-bit mode timer reads and writes tmr0h is not the high byte of the timer/counter in 16-bit mode, but is actually a buffered version of the high byte of timer0 (refer to figure 11-2). the high byte of the timer0 counter/timer is not directly readable nor writable. tmr0h is updated with the contents of the high byte of timer0 during a read of tmr0l. this pro- vides the ability to read all 16 bits of timer0 without having to verify that the read of the high and low byte were valid due to a rollover between successive reads of the high and low byte. a write to the high byte of timer0 must also take place through the tmr0h buffer register. timer0 high byte is updated with the contents of tmr0h when a write occurs to tmr0l. this allows all 16 bits of timer0 to be updated at once. table 11-1: registers associated with timer0 note: writing to tmr0 when the prescaler is assigned to timer0 will clear the prescaler count, but will not change the prescaler assignment. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets tmr0l timer0 module low byte register xxxx xxxx uuuu uuuu tmr0h timer0 module high byte register 0000 0000 0000 0000 intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u t0con tmr0on t016bit t0cs t0se psa t0ps2 t0ps1 t0ps0 1111 1111 1111 1111 trisa ra7 (1) ra6 (1) porta data direction register 1111 1111 1111 1111 legend: x = unknown, u = unchanged, ? = unimplemented locations read as ? 0 ?. shaded cells are not used by timer0. note 1: ra6 and ra7 are enabled as i/o pins depending on the oscillator mode selected in configuration word 1h.
pic18f2331/2431/4331/4431 ds39616b-page 136 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 137 pic18f2331/2431/4331/4431 12.0 timer1 module the timer1 module timer/counter has the following features:  16-bit timer/counter (two 8-bit registers; tmr1h and tmr1l)  readable and writable (both registers)  internal or external clock select  interrupt-on-overflow from ffffh to 0000h  reset from ccp module special event trigger  status of system clock operation figure 12-1 is a simplified block diagram of the timer1 module. register 12-1 details the timer1 control register. this register controls the operating mode of the timer1 module, and contains the timer1 oscillator enable bit (t1oscen). timer1 can be enabled or disabled by setting or clearing control bit tmr1on (t1con<0>). the timer1 oscillator can be used as a secondary clock source in power-managed modes. when the t1run bit is set, the timer1 oscillator provides the system clock. if the fail-safe clock monitor is enabled and the timer1 oscillator fails while providing the system clock, polling the t1run bit will indicate whether the clock is being provided by the timer1 oscillator or another source. timer1 can also be used to provide real-time clock (rtc) functionality to applications with only a minimal addition of external components and code overhead. register 12-1: t1con: timer1 control register r/w-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on bit 7 bit 0 bit 7 rd16: 16-bit read/write mode enable bit 1 = enables register read/write of timer1 in one 16-bit operation 0 = enables register read/write of timer1 in two 8-bit operations bit 6 t1run: timer1 system clock status bit 1 = system clock is derived from timer1 oscillator 0 = system clock is derived from another source bit 5-4 t1ckps1:t1ckps0: timer1 input clock prescale select bits 11 =1:8 prescale value 10 =1:4 prescale value 01 =1:2 prescale value 00 =1:1 prescale value bit 3 t1oscen: timer1 oscillator enable bit 1 = timer1 oscillator is enabled 0 = timer1 oscillator is shut-off the oscillator inverter and feedback resistor are turned off to eliminate power drain. bit 2 t1sync : timer1 external clock input synchronization select bit when tmr1cs = 1 (external clock): 1 = do not synchronize external clock input 0 = synchronize external clock input when tmr1cs = 0 (internal clock): this bit is ignored. timer1 uses the internal clock when tmr1cs = 0 . bit 1 tmr1cs: timer1 clock source select bit 1 = external clock from pin rc0/t1oso/t1cki (on the rising edge) 0 = internal clock (f osc /4) bit 0 tmr1on: timer1 on bit 1 = enables timer1 0 = stops timer1 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 138 preliminary ? 2003 microchip technology inc. 12.1 timer1 operation timer1 can operate in one of these modes: as a timer  as a synchronous counter  as an asynchronous counter the operating mode is determined by the clock select bit, tmr1cs (t1con<1>). when tmr1cs = 0 , timer1 increments every instruc- tion cycle. when tmr1cs = 1 , timer1 increments on every rising edge of the external clock input or the timer1 oscillator, if enabled. when the timer1 oscillator is enabled (t1oscen is set), the rc1/t1osi/ccp2/flta and rc0/t1oso/ t1cki pins become inputs. that is, the trisc1:trisc0 value is ignored, and the pins are read as ? 0 ?. timer1 also has an internal ?reset input?. this reset can be generated by the ccp module (see section 15.4.4 ?special event trigger? ). figure 12-1: timer1 block diagram figure 12-2: timer1 block diag ram: 16-bit read/write mode t1osc tmr1h tmr1l t1sync tmr1cs t1ckps1:t1ckps0 peripheral clocks f osc /4 internal clock tmr1on on/off prescaler 1, 2, 4, 8 synchronize det 1 0 0 1 synchronized clock input 2 tmr1if overflow tmr1 clr ccp special event trigger t1oscen enable oscillator (1) interrupt flag bit note 1: when enable bit t1oscen is cleared, the inverter and feedback re sistor are turned off. this eliminates power drain. t1osi t1cki/t1oso timer 1 tmr1l t1osc t1sync tmr1cs t1ckps1:t1ckps0 peripheral clocks t1oscen enable oscillator (1) tmr1if overflow interrupt f osc /4 internal clock tmr1on on/off prescaler 1, 2, 4, 8 synchronize det 1 0 0 1 synchronized clock input 2 t1cki/t1oso t1osi tmr1 flag bit note 1: when enable bit t1oscen is cleared, the inverter and feedback re sistor are turned off. this eliminates power drain. high byte data bus<7:0> 8 tmr1h 8 8 8 read tmr1l write tmr1l clr ccp special event trigger
? 2003 microchip technology inc. preliminary ds39616b-page 139 pic18f2331/2431/4331/4431 12.2 timer1 oscillator a crystal oscillator circuit is built-in between pins t1osi (input) and t1oso (amplifier output). it is enabled by setting control bit t1oscen (t1con<3>). the oscilla- tor is a low-power oscillator rated for 32 khz crystals. it will continue to run during all power-managed modes. the circuit for a typical lp oscillator is shown in figure 12-3. table 12-1 shows the capacitor selection for the timer1 oscillator. the user must provide a software time delay to ensure proper start-up of the timer1 oscillator. figure 12-3: external components for the timer1 lp oscillator table 12-1: capacitor selection for the timer oscillator 12.3 timer1 oscillator layout considerations the timer1 oscillator for pic18f2331/2431/4331/4431 devices incorporates an additional low-power feature. when this option is selected, it allows the oscillator to automatically reduce its power consumption when the microcontroller is in sleep mode. during normal device operation, the oscillator draws full current. as high noise environments may cause excessive oscillator instability in sleep mode, this option is best suited for low noise applications where power conservation is an important design consideration. the low-power option is enabled by clearing the t1oscmx bit (config3l<5>). by default, the option is disabled, which results in a more-or-less constant current draw for the timer1 oscillator. due to the low power nature of the oscillator, it may also be sensitive to rapidly changing signals in close proximity. the oscillator circuit, shown in figure 12-3, should be located as close as possible to the microcontroller. there should be no circuits passing within the oscillator circuit boundaries other than v ss or v dd . if a high-speed circuit must be located near the oscilla- tor (such as the ccp1 pin in output compare or pwm mode, or the primary oscillator using the osc2 pin), a grounded guard ring around the oscillator circuit, as shown in figure 12-4, may be helpful when used on a single sided pcb, or in addition to a ground plane. figure 12-4: oscillator circuit with grounded guard ring osc type freq c1 c2 lp 32 khz 27 pf (1) 27 pf (1) note 1: microchip suggests this value as a starting point in validating the oscillator circuit. 2: higher capacitance increases the stability of the oscillator, but also increases the start-up time. 3: since each resonator/crystal has its own characteristics, the user should consult the resonator/crystal manufacturer for appropriate values of external components. 4: capacitor values are for design guidance only. note: see the notes with table 12-1 for additiona l information about capacitor selection. c1 c2 xtal pic18fxxxx t1osi t1oso 32.768 khz 33 pf 33 pf v dd osc1 v ss osc2 rc0 rc1 rc2 note: not drawn to scale.
pic18f2331/2431/4331/4431 ds39616b-page 140 preliminary ? 2003 microchip technology inc. 12.4 timer1 interrupt the tmr1 register pair (tmr1h:tmr1l) increments from 0000h to ffffh and rolls over to 0000h. the timer1 interrupt, if enabled, is generated on overflow, which is latched in interrupt flag bit, tmr1if (pir1<0>). this interrupt can be enabled/disabled by setting/clearing timer1 interrupt enable bit, tmr1ie (pie1<0>). 12.5 resetting timer1 using a ccp trigger output if the ccp module is configured in compare mode to generate a ?special event trigger? (ccp1m3:ccp1m0 = 1011 ), this signal will reset timer1 and start an a/d conversion if the a/d module is enabled (see section 15.4.4 ?special event trigger? for more information.). timer1 must be configured for either timer or synchro- nized counter mode to take advantage of this feature. if timer1 is running in asynchronous counter mode, this reset operation may not work. in the event that a write to timer1 coincides with a special event trigger from ccp1, the write will take precedence. in this mode of operation, the ccpr1h:ccpr1l regis- ters pair effectively becomes the period register for timer1. 12.6 timer1 16-bit read/write mode timer1 can be configured for 16-bit reads and writes (see figure 12-2). when the rd16 control bit (t1con<7>) is set, the address for tmr1h is mapped to a buffer register for the high byte of timer1. a read from tmr1l will load the contents of the high byte of timer1 into the timer1 high byte buffer. this provides the user with the ability to accurately read all 16 bits of timer1 without having to determine whether a read of the high byte, followed by a read of the low byte, is valid, due to a rollover between reads. a write to the high byte of timer1 must also take place through the tmr1h buffer register. timer1 high byte is updated with the contents of tmr1h when a write occurs to tmr1l. this allows a user to write all 16 bits to both the high and low bytes of timer1 at once. the high byte of timer1 is not directly readable or writ- able in this mode. all reads and writes must take place through the timer1 high byte buffer register. writes to tmr1h do not clear the timer1 prescaler. the prescaler is only cleared on writes to tmr1l. 12.7 using timer1 as a real-time clock adding an external lp oscillator to timer1 (such as the one described in section 12.2 ?timer1 oscillator? ), gives users the option to include rtc functionality to their applications. this is accomplished with an inexpensive watch crystal to provide an accurate time base, and several lines of application code to calculate the time. when operating in sleep mode and using a battery or supercapacitor as a power source, it can completely eliminate the need for a separate rtc device and battery backup. the application code routine rtcisr , shown in example 12-1, demonstrates a simple method to increment a counter at one-second intervals using an interrupt service routine. incrementing the tmr1 register pair to overflow triggers the interrupt and calls the routine, which increments the seconds counter by one. additional counters for minutes and hours are incremented as the previous counter overflow. since the register pair is 16-bits wide, counting up to overflow the register directly from a 32.768 khz clock would take 2 seconds. to force the overflow at the required one-second intervals, it is necessary to pre- load it; the simplest method is to set the msbit of tmr1h with a bsf instruction. note that the tmr1l register is never preloaded or altered; doing so may introduce cumulative error over many cycles. for this method to be accurate, timer1 must operate in asynchronous mode, and the timer1 overflow interrupt must be enabled (pie1<0> = 1 ), as shown in the rou- tine rtcinit . the timer1 oscillator must also be enabled and running at all times. note: the special event triggers from the ccp1 module will not set interrupt flag bit tmr1if (pir1<0>).
? 2003 microchip technology inc. preliminary ds39616b-page 141 pic18f2331/2431/4331/4431 example 12-1: implementing a real-time clock using a timer1 interrupt service table 12-2: registers associated with timer1 as a timer/counter rtcinit movlw 0x80 ; preload tmr1 register pair movwf tmr1h ; for 1 second overflow clrf tmr1l movlw b?00001111? ; configure for external clock, movwf t1osc ; asynchronous operation, external oscillator clrf secs ; initialize timekeeping registers clrf mins ; movlw .12 movwf hours bsf pie1, tmr1ie ; enable timer1 interrupt return rtcisr bsf tmr1h, 7 ; preload for 1 sec overflow bcf pir1, tmr1if ; clear interrupt flag incf secs, f ; increment seconds movlw .59 ; 60 seconds elapsed? cpfsgt secs return ; no, done clrf secs ; clear seconds incf mins, f ; increment minutes movlw .59 ; 60 minutes elapsed? cpfsgt mins return ; no, done clrf mins ; clear minutes incf hours, f ; increment hours movlw .23 ; 24 hours elapsed? cpfsgt hours return ; no, done movlw .01 ; reset hours to 1 movwf hours return ; done name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif -000 000x 0000 000u pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if -000 0000 -000 0000 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie -000 0000 -000 0000 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip 1111 1111 -111 1111 tmr1l holding register for the least significant byte of the 16-bit tmr1 register xxxx xxxx uuuu uuuu tmr1h holding register for the most significant byte of the 16-bit tmr1 register xxxx xxxx uuuu uuuu t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 0000 0000 u0uu uuuu legend: x = unknown, u = unchanged, ? = unimplemented, read as ? 0 ?. shaded cells are not used by the timer1 module.
pic18f2331/2431/4331/4431 ds39616b-page 142 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 143 pic18f2331/2431/4331/4431 13.0 timer2 module the timer2 module timer has the following features:  8-bit timer (tmr2 register)  8-bit period register (pr2)  readable and writable (both registers)  software programmable prescaler (1:1, 1:4, 1:16)  software programmable postscaler (1:1 to 1:16)  interrupt on tmr2 match with pr2  ssp module optional use of tmr2 output to generate clock shift timer2 has a control register shown in register 13-1. tmr2 can be shut off by clearing control bit tmr2on (t2con<2>) to minimize power consumption. figure 13-1 is a simplified block diagram of the timer2 module. register 13-1 shows the timer2 control register. the prescaler and postscaler selection of timer2 are controlled by this register. 13.1 timer2 operation timer2 can be used as the pwm time base for the pwm mode of the ccp module. the tmr2 register is readable and writable, and is cleared on any device reset. the input clock (f osc /4) has a prescale option of 1:1, 1:4 or 1:16, selected by control bits t2ckps1:t2ckps0 (t2con<1:0>). the match out- put of tmr2 goes through a 4-bit postscaler (which gives a 1:1 to 1:16 scaling inclusive) to generate a tmr2 interrupt (latched in flag bit tmr2if, (pir1<1>)). the prescaler and postscaler counters are cleared when any of the following occurs:  a write to the tmr2 register  a write to the t2con register  any device reset (power-on reset, mclr reset, watchdog timer reset or brown-out reset) tmr2 is not cleared when t2con is written. register 13-1: t2con: timer2 cont rol register u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? toutps3 toutps2 toutps1 toutps0 tmr2on t2ckps1 t2ckps0 bit 7 bit 0 bit 7 unimplemented: read as ? 0 ? bit 6-3 toutps3:toutps0: timer2 output postscale select bits 0000 = 1:1 postscale 0001 =1:2 postscale    1111 =1:16 postscale bit 2 tmr2on: timer2 on bit 1 = timer2 is on 0 = timer2 is off bit 1-0 t2ckps1:t2ckps0: timer2 clock prescale select bits 00 = prescaler is 1 01 = prescaler is 4 1x = prescaler is 16 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 144 preliminary ? 2003 microchip technology inc. 13.2 timer2 interrupt the timer2 module has an 8-bit period register, pr2. timer2 increments from 00h until it matches pr2 and then resets to 00h on the next increment cycle. pr2 is a readable and writable register. the pr2 register is initialized to ffh upon reset. 13.3 output of tmr2 the output of tmr2 (before the postscaler) is fed to the synchronous serial port module, which optionally uses it to generate the shift clock. figure 13-1: timer2 block diagram table 13-1: registers associated with timer2 as a timer/counter comparator tmr2 sets flag tmr2 output (1) reset postscaler prescaler pr2 2 f osc /4 1:1 to 1:16 1:1, 1:4, 1:16 eq 4 bit tmr2if note 1: tmr2 register output can be software selected by the ssp module as a baud clock. toutps3:toutps0 t2ckps1:t2ckps0 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if -000 0000 -000 0000 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie -000 0000 -000 0000 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip -111 1111 -111 1111 tmr2 timer2 module register 0000 0000 0000 0000 t2con ? toutps3 toutps2 toutps1 toutps0 tmr2on t2ckps1 t2ckps0 -000 0000 -000 0000 pr2 timer2 period register 1111 1111 1111 1111 legend: x = unknown, u = unchanged, ? = unimplemented read as ? 0 ?. shaded cells are not used by the timer2 module.
? 2003 microchip technology inc. preliminary ds39616b-page 145 pic18f2331/2431/4331/4431 14.0 timer5 module the timer5 module implements these features:  16-bit timer/counter operation  synchronous and asynchronous counter modes  continuous and single-shot operating modes  four programmable prescaler values (1:1 to 1:8)  interrupt generated on period match  special event trigger reset function  double-buffered registers  operation during sleep  cpu wake-up from sleep  selectable hardware reset input with a wake-up feature timer5 is a general-purpose timer/counter that incorpo- rates additional features for use with the motion feed- back module (see section 16.0 ?motion feedback module? ). it may also be used as a general-purpose timer or a special event trigger delay timer. when used as a general-purpose timer, it can be configured to gen- erate a delayed special event trigger (e.g., an adc special event trigger) using a pre-programmed period delay. timer5 is controlled through the timer5 control regis- ter (t5con), shown in register 14-1. the timer can be enabled or disabled by setting or clearing the control bit tmr5on (t5con<0>). a block diagram of timer5 is shown in figure 14-1. register 14-1: t5con: timer5 cont rol register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 t5sen resen t5mod t5ps1 t5ps0 t5sync tmr5cs tmr5on bit 7 bit 0 bit 7 t5sen: timer5 sleep enable bit (1) 1 = timer5 enabled during sleep 0 = timer5 disabled during sleep bit 6 resen : special event reset enable bit 1 = special event reset disabled 0 = special event reset enabled bit 5 t5mod: timer5 mode bit 1 = single-shot mode enabled 0 = continuous count mode enabled bit 4:3 t5ps1:t5ps0: timer5 input clock prescale select bits 11 =1:8 10 =1:4 01 =1:2 00 =1:1 bit 2 t5sync : timer5 external clock input synchronization select bit when tmr5cs = 1 : 1 = do not synchronize external clock input 0 = synchronize external clock input when tmr5cs = 0 : this bit is ignored. timer5 uses the internal clock when tmr5cs = 0 bit 1 tmr5cs: timer5 clock source select bit 1 = external clock from pin t5cki 0 = internal clock (t cy ) bit 0 tmr5on: timer5 on bit 1 = timer5 enabled 0 = timer5 disabled note 1: for timer5 to operate during sleep mode, t5sync must be set. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 146 preliminary ? 2003 microchip technology inc. figure 14-1: timer5 block diagram (16-bit read/write mode shown) 14.1 timer5 operation timer5 combines two 8-bit registers to function as a 16- bit timer. the tmr5l register is the actual low byte of the timer; it can be read and written to directly. the high byte is contained in an unmapped register; it is read and written to through tmr5h, which serves as a buffer. each register increments from 00h to ffh. a second register pair, pr5h and pr5l, serves as a period register; it sets the maximum count for the tmr5 register pair. when tmr5 reaches the value of pr5, the timer rolls over to 00h and sets the tmr5if interrupt flag. a simplified block diagram of the timer5 module is shown in figure 2-1. timer5 supports three configurations:  16-bit synchronous timer  16-bit synchronous counter  16-bit asynchronous counter in synchronous timer configuration, the timer is clocked by the internal device clock. the optional timer5 prescaler divides the input by 2, 4, 8, or not at all (1:1). the tmr5 register pair increments on q1. clearing tmr5cs (= 0 ) selects the internal device clock as the timer sampling clock. in synchronous counter configuration, the timer is clocked by the external clock (t5cki) with the optional prescaler. the external t5cki is selected by setting the tmr5cs bit (tmr5cs = 1 ); the internal clock is selected by clearing tmr5cs. the external clock is synchronized to the internal clock by clearing the t5sync bit. the input on t5cki is sampled on every q2 and q4 of the internal clock. the low to rise transition is decoded on three adjacent samples and t5sync tmr5cs t5ps1:t5ps0 sleep input f osc /4 internal clock prescaler 1, 2, 4, 8 synchronize detect 1 0 2 t5cki 1 0 tmr5l internal data bus tmr5 tmr5h high byte 8 8 write tmr5l read tmr5l 8 tmr5on on/off timer5 noise filter pr5l 16 pr5h 8 8 comparator 16 8 1 0 tmr5 pr5 reset logic special event logic timer5 reset set tmr5if timer5 reset (external) special event trigger output special event trigger input from ic1 note: the timer5 may be used as a general purpose timer and as the time base resource to the motion feedback module (input capture or quadrature encoder interface).
? 2003 microchip technology inc. preliminary ds39616b-page 147 pic18f2331/2431/4331/4431 the timer5 is incremented on the next q1. the t5cki minimum pulse width high and low time must be greater than t cy /2. in asynchronous counter configuration, timer5 is clocked by the external clock (t5cki) with the optional prescaler. in this mode, t5cki is not synchronized to the internal clock. by setting tmr5cs, the external input clock (t5cki) can be used as the counter sam- pling clock. when t5sync is set, the external clock is not synchronized to the internal device clock. the timer count is not reset automatically when the module is disabled. the user may write the counter register to initialize the counter. 14.1.1 continuous and single-shot operation timer5 has two operating modes: continuous-count and single-shot. continuous-count mode is selected by clearing the t5mod control bit (= 0 ). in this mode, the timer5 time base will start incrementing according to the prescaler settings until a tmr5/pr5 match occurs, or until tmr5 rolls over (ffffh to 0000h). the tmr5if interrupt flag is set, the tmr5 register is reset on the following input clock edge, and the timer continues to count for as long as the tmr5on bit remains set. single-shot mode is selected by setting t5mod (= 1 ). in this mode, the timer5 time base begins to increment according to the prescaler settings until a tmr5/pr5 match occurs. this causes the tmr5if interrupt flag to be set, the tmr5 register pair to be cleared on the following input clock edge, and the tmr5on bit to be cleared by the hardware to halt the timer. the timer5 time base can only start incrementing in single-shot mode under two conditions: 1. timer5 is enabled (tmr5on is set), or 2. timer5 is disabled, and a special event reset trigger is present on the timer5 reset input. (see section 14.7 ?timer5 special event reset input? for additional information). 14.2 16-bit read/write and write modes as noted, the actual high byte of the timer5 register pair is mapped to tmr5h, which serves as a buffer. reading tmr5l will load the contents of the high byte of the register pair into the tmr5h register. this allows the user to accurately read all 16 bits of the register pair, without having to determine whether a read of the high byte followed by the low byte is valid due to a rollover between reads. since the actual high byte of the timer5 register pair is not directly readable or writable, it must be read and written to through the timer5 high byte buffer register (tmr5h). the t5 high byte is updated with the con- tents of tmr5h when a write occurs to tmr5l. this allows a user to write all 16 bits to both the high and low bytes of timer5 at once. writes to tmr5h do not clear the timer5 prescaler. the prescaler is only cleared on writes to tmr5l. 14.2.1 16-bit read-modify-write read-modify-write instructions like bsf and bcf will read the contents of a register, make the appropriate changes, and place the result back into the register. the write portion of a read-modify-write instruction of tmr5h will not update the contents of the high byte of tmr5 until a write of tmr5l takes place. only then will the contents of tmr5h be placed into the high byte of tmr5. 14.3 timer5 prescaler the timer5 clock input (either t cy or the external clock) may be divided by using the timer5 programmable prescaler. the prescaler control bits t5ps1:t5ps0 (t5con<4:3>) select a prescale factor of 2, 4, 8 or no prescale. the timer5 prescaler is cleared by any of the following:  a write to the timer5 register  disabling timer5 (tmr5on = 0 )  a device reset such as master clear, por or bor 14.4 noise filter the timer5 module includes an optional input noise filter, designed to reduce spurious signals in noisy operating environments. the filter ensures that the input is not permitted to change until a stable value has been registered for three consecutive sampling clock cycles. the noise filter is part of the input filter network associ- ated with the motion feedback module (see section 16.0 ?motion feedback module? ). all of the filters are controlled using the digital filter control (dfltcon) register (register 16-3). the timer5 filter can be individually enabled or disabled by setting or clearing the flt4en bit (dfltcon<7>). it is disabled on all bor and bor resets. for additional information, refer to section 16.3 ?noise filters? in the motion feedback module. note: the timer5 module does not prevent writes to the pr5 registers (pr5h:pr5l) while the timer is enabled. writing to pr5 while the timer is enabled may result in unexpected period match events. note: writing to the t5con register does not clear the timer5.
pic18f2331/2431/4331/4431 ds39616b-page 148 preliminary ? 2003 microchip technology inc. 14.5 timer5 interrupt timer5 has the ability to generate an interrupt on a period match. when the pr5 register is loaded with a new period value (00ffh), the timer5 time base incre- ments until its value is equal to the value of pr5. when a match occurs, the timer5 interrupt is generated on the rising edge of q4; tmr5if is set on the next t cy . the interrupt latency (i.e., the time elapsed from the moment timer5 rolls over until tmr5if is set) will not exceed 1 t cy . when the timer5 clock input is prescaled and a tmr5/pr5 match occurs, the interrupt will be generated on the first q4 rising edge after tmr5 resets. 14.6 timer5 special event trigger output a timer5 special event trigger is generated on a tmr5/ pr5 match. the special event trigger is generated on the falling edge of q3. timer5 must be configured for either synchronous mode (counter or timer) to take advantage of the special event trigger feature. if timer5 is running in asynchronous counter mode, the special event trigger may not work and should not be used. 14.7 timer5 special event reset input in addition to the special event output, timer5 has a special event reset input that may be used with input capture channel 1 (ic1) of the motion feedback module. to use the special event reset, the capture 1 control register cap1con must be configured for one of the special event trigger modes (cap1m3:cap1m0 = 1110 or 1111 ). the special event reset trigger can be disabled by setting the resen control bit (t5con<6>). the special event reset resets the timer5 time base. this reset occurs in either continuous-count or single- shot modes. 14.7.1 wake-up on ic1 edge the timer5 special event reset input can act as a timer5 wake-up and a start-up pulse. timer5 must be in single-shot mode and disabled (tmr5on = 0 ). an active edge on the cap1 input pin will set tmr5on; the timer is subsequently incremented on the next fol- lowing clock according to the prescaler and the timer5 clock settings. a subsequent hardware time-out (such as tmr5/pr5 match) will clear the tmr5on bit and stop the timer. 14.7.2 delayed-action event trigger an active edge on cap1 can also be used to initiate some later action delayed by the timer5 time base. in this case, timer5 increments as before after being triggered. when the hardware time-out occurs, the special event trigger output is generated and used to trigger another action, such as an a/d conversion. this allows a given hardware action to be referenced from a capture edge on cap1 and delayed by the timer. the event timing for the delayed action event trigger is discussed further in section 16.1 ?input capture? . 14.7.3 special event reset while timer5 is incrementing in the event that a bus write to timer5 coincides with a special event reset trigger, the bus write will always take precedence over special event reset trigger. 14.8 operation in sleep mode when timer5 is configured for asynchronous opera- tion, it will continue to increment each timer clock (or prescale multiple of clocks). executing the sleep instruction will either stop the timer or let the timer con- tinue, depending on the setting of the timer5 sleep enable bit, t5se. if t5se is set (= 1 ), the timer contin- ues to run when the sleep instruction is executed and the external clock is selected (tmr5cs = 1 ). if t5se is cleared, the timer stops when a sleep instruction is executed, regardless of the state of the gtpcs bit. to summarize, timer5 will continue to increment when a sleep instruction is executed only if all of these bits are set: tmr5on t5se tmr5cs  t5sync 14.8.1 interrupt detect in sleep mode when configured as described above, timer5 will continue to increment on each rising edge on t5cki while in sleep mode. when a tmr5/pr5 match occurs, an interrupt is generated which can wake the part.
? 2003 microchip technology inc. preliminary ds39616b-page 149 pic18f2331/2431/4331/4431 table 14-1: registers associated with timer5 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u ipr3 ? ? ? ptip ic3drip ic2qeip ic1ip tmr5ip ---1 1111 ---1 1111 pie3 ? ? ? ptie ic3drie ic2qeie ic1ie tmr5ie ---0 0000 ---0 0000 pir3 ? ? ? ptif ic3drif ic2qeif ic1if tmr5if ---0 0000 ---0 0000 tmr5h timer5 register high byte xxxx xxxx uuuu uuuu tmr5l timer5 register low byte xxxx xxxx uuuu uuuu pr5h timer5 period register high byte 1111 1111 1111 1111 pr5l timer5 period register low byte 1111 1111 1111 1111 t5con t5sen resen t5mod t5ps1 t5ps0 t5sync tmr5cs tmr5on 0000 0000 0000 0000 cap1con ? cap1ren ? ? cap1m3 cap1m2 cap1m1 cap1m0 -1-- 0000 -1-0 0000 dfltcon ?flt4en flt3en flt2en flt1en fltck2 fltck1 fltck0 -000 0000 -000 0000 legend: x = unknown, u = unchanged, ? = unimplemented.
pic18f2331/2431/4331/4431 ds39616b-page 150 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 151 pic18f2331/2431/4331/4431 15.0 capture/compare/pwm (ccp) modules the ccp (capture/compare/pwm) module contains a 16-bit register that can operate as a 16-bit capture reg- ister, a 16-bit compare register or a pwm master/slave duty cycle register. table 15-1 shows the timer resources required for each of the ccp module modes. the operation of ccp1 is identical to that of ccp2, with the exception of the special event trigger. therefore, operation of a ccp module is described with respect to ccp1, except where noted. register 15-1: ccpxcon: ccp module cont rol register u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? dcxb1 dcxb0 ccpxm3 ccpxm2 ccpxm1 ccpxm0 bit 7 bit 0 bit 7-6 unimplemented: read as ? 0 ? bit 5-4 dcxb1:dcxb0: pwm duty cycle bit1 and bit0 capture mode: unused compare mode: unused pwm mode: these bits are the two lsbs (bit 1 and bit 0) of the 10-bit pwm duty cycle. the upper eight bits (dcx9:dcx2) of the duty cycle are found in ccprxl. bit 3-0 ccpxm3:ccpxm0 : ccpx mode select bits 0000 =capture/compare/pwm disabled (resets ccpx module) 0001 =reserved 0010 =compare mode, toggle output on match (ccpxif bit is set) 0011 =reserved 0100 =capture mode, every falling edge 0101 =capture mode, every rising edge 0110 =capture mode, every 4th rising edge 0111 =capture mode, every 16th rising edge 1000 =compare mode, initialize ccp pin low, on compare match force ccp pin high (ccpxif bit is set) 1001 =compare mode, initialize ccp pin high, on compare match force ccp pin low (ccpxif bit is set) 1010 =compare mode, generate software interrupt-on-compare match (ccpxif bit is set, ccp pin is unaffected) 1011 =compare mode, trigger special event (ccp2if bit is set) 11xx =pwm mode legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 152 preliminary ? 2003 microchip technology inc. 15.1 ccp1 module capture/compare/pwm register 1 (ccpr1) is comprised of two 8-bit registers: ccpr1l (low byte) and ccpr1h (high byte). the ccp1con register controls the operation of ccp1. all are readable and writable. table 15-1: ccp mode ? timer resource 15.2 ccp2 module capture/compare/pwm register2 (ccpr2) is com- prised of two 8-bit registers: ccpr2l (low byte) and ccpr2h (high byte). the ccp2con register controls the operation of ccp2. all are readable and writable. ccp mode timer resource capture compare pwm timer1 timer1 timer2
? 2003 microchip technology inc. preliminary ds39616b-page 153 pic18f2331/2431/4331/4431 15.3 capture mode in capture mode, ccpr1h:ccpr1l captures the 16- bit value of the tmr1 register when an event occurs on pin rc2/ccp1. an event is defined as one of the following:  every falling edge  every rising edge  every 4th rising edge  every 16th rising edge the event is selected by control bits ccp1m3:ccp1m0 (ccp1con<3:0>). when a capture is made, the interrupt request flag bit ccp1if (pir1<2>) is set; it must be cleared in software. if another capture occurs before the value in register ccpr1 is read, the old captured value is overwritten by the new captured value. 15.3.1 ccp pin configuration in capture mode, the rc2/ccp1 pin should be configured as an input by setting the trisc<2> bit. 15.3.2 timer1 mode selection timer 1 must be running in timer mode or synchro- nized counter mode to be used with the capture fea- ture. in asynchronous counter mode, the capture operation may not work. 15.3.3 software interrupt when the capture mode is changed, a false capture interrupt may be generated. the user should keep bit ccp1ie (pie1<2>) clear to avoid false interrupts and should clear the flag bit, ccp1if, following any such change in operating mode. 15.3.4 ccp prescaler there are four prescaler settings, specified by bits ccp1m3:ccp1m0. whenever the ccp module is turned off or the ccp module is not in capture mode, the prescaler counter is cleared. this means that any reset will clear the prescaler counter. switching from one capture prescaler to another may generate an interrupt. also, the prescaler counter will not be cleared, therefore, the first capture may be from a non-zero prescaler. example 15-1 shows the recom- mended method for switching between capture pres- calers. this example also clears the prescaler counter and will not generate the ?false? interrupt. example 15-1: changing between capture prescalers figure 15-1: capture mode operat ion block diagram note: if the rc2/ccp1 is configured as an out- put, a write to the port can cause a capture condition. clrf ccp1con, f ; turn ccp module off movlw new_capt_ps ; load wreg with the ; new prescaler mode ; value and ccp on movwf ccp1con ; load ccp1con with ; this value ccpr1h ccpr1l tmr1h tmr1l set flag bit ccp1if q?s ccp1con<3:0> ccp1 pin prescaler 1, 4, 16 and edge detect tmr1 enable ccpr2h ccpr2l tmr1h tmr1l set flag bit ccp2if q?s ccp2con<3:0> ccp2 pin prescaler 1, 4, 16 and edge detect tmr1 enable
pic18f2331/2431/4331/4431 ds39616b-page 154 preliminary ? 2003 microchip technology inc. 15.4 compare mode in compare mode, the 16-bit ccpr1 (ccpr2) register value is constantly compared against the tmr1 register pair value. when a match occurs, the rc2/ ccp1 (rc1/ccp2) pin:  is driven high  is driven low  toggles output (high-to-low or low-to-high)  remains unchanged (interrupt only) the action on the pin is based on the value of control bits ccp1m3:ccp1m0 (ccp2m3:ccp2m0). at the same time, interrupt flag bit ccp1if (ccp2if) is set. 15.4.1 ccp pin configuration the user must configure the ccpx pin as an output by clearing the appropriate trisc bit. 15.4.2 timer1 mode selection timer1 must be running in timer mode or synchro- nized counter mode if the ccp module is using the compare feature. in asynchronous counter mode, the compare operation may not work. 15.4.3 software interrupt mode when generate software interrupt is chosen, the ccp1 pin is not affected. only a ccp interrupt is generated (if enabled). 15.4.4 special event trigger in this mode, an internal hardware trigger is generated, which may be used to initiate an action. the special event trigger output of ccp1 resets the tmr1 register pair. this allows the ccpr1 register to effectively be a 16-bit programmable period register for timer1. the special trigger output of ccp2 resets the tmr1 register pair. additionally, the ccp2 special event trigger will start an a/d conversion if the a/d module is enabled. figure 15-2: compare mode operation block diagram note: clearing the ccp1con register will force the rc2/ccp1 compare output latch to the default low level. this is not the portc i/o data latch. note: the special event trigger from the ccp2 module will not set the timer1 interrupt flag bit. ccpr1h ccpr1l tmr1h tmr1l comparator qs r output logic special event trigger set flag bit ccp1if match rc2/ccp1 pin trisc<2> ccp1con<3:0> mode select output enable special event trigger will: reset timer1, but not set timer1 interrupt flag bit, and set bit go/done (adcon0<2>), which starts an a/d conversion (ccp2 only) ccpr2h ccpr2l comparator qs r output logic special event trigger set flag bit ccp2if match rc1/ccp2 pin trisc<1> ccp2con<3:0> mode select output enable
? 2003 microchip technology inc. preliminary ds39616b-page 155 pic18f2331/2431/4331/4431 table 15-2: registers associated with capture, compare and timer1 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if -000 0000 -000 0000 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie -000 0000 -000 0000 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip -111 1111 -111 1111 trisc portc data direction register 1111 1111 1111 1111 tmr1l holding register for the least significant byte of the 16-bit tmr1 register xxxx xxxx uuuu uuuu tmr1h holding register for the most significant byte of the 16-bit tmr1 register xxxx xxxx uuuu uuuu t1con rd16 t1run t1ckps1 t1ckps0 t1oscen t1sync tmr1cs tmr1on 0000 0000 uuuu uuuu ccpr1l capture/compare/pwm register1 (lsb) xxxx xxxx uuuu uuuu ccpr1h capture/compare/pwm register1 (msb) xxxx xxxx uuuu uuuu ccp1con ? ? dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 --00 0000 --00 0000 ccpr2l capture/compare/pwm register2 (lsb) xxxx xxxx uuuu uuuu ccpr2h capture/compare/pwm register2 (msb) xxxx xxxx uuuu uuuu ccp2con ? ? dc2b1 dc2b0 ccp2m3 ccp2m2 ccp2m1 ccp2m0 --00 0000 --00 0000 pir2 oscfif cmif ? eeif bclif lvdif tmr3if ccp2if 00-0 0000 00-0 0000 pie2 oscfie cmie ? eeie bclie lvdie tmr3ie ccp2ie 00-0 0000 00-0 0000 ipr2 oscfip cmip ? eeip bclip lvdip tmr3ip ccp2ip 11-1 1111 11-1 1111 legend: x = unknown, u = unchanged, ? = unimplemented, read as ? 0 ?. shaded cells are not used by capture and timer1.
pic18f2331/2431/4331/4431 ds39616b-page 156 preliminary ? 2003 microchip technology inc. 15.5 pwm mode in pulse width modulation (pwm) mode, the ccp1 pin produces up to a 10-bit resolution pwm output. since the ccp1 pin is multiplexed with the portc data latch, the trisc<2> bit must be cleared to make the ccp1 pin an output. figure 15-3 shows a simplified block diagram of the ccp module in pwm mode. for a step-by-step procedure on how to set up the ccp module for pwm operation, see section 15.5.3 ?setup for pwm operation? . figure 15-3: simplified pwm block diagram a pwm output (figure 15-4) has a time base (period) and a time that the output is high (duty cycle). the frequency of the pwm is the inverse of the period (1/period). figure 15-4: pwm output 15.5.1 pwm period the pwm period is specified by writing to the pr2 register. the pwm period can be calculated using the following equation. equation 15-1: pwm frequency is defined as 1/[pwm period]. when tmr2 is equal to pr2, the following three events occur on the next increment cycle: tmr2 is cleared  the ccp1 pin is set (if pwm duty cycle = 0%, the ccp1 pin will not be set)  the pwm duty cycle is copied from ccpr1l into ccpr1h 15.5.2 pwm duty cycle the pwm duty cycle is specified by writing to the ccpr1l register and to the ccp1con<5:4> bits. up to 10-bit resolution is available. the ccpr1l contains the eight msbs and the ccp1con<5:4> contains the two lsbs. this 10-bit value is represented by ccpr1l:ccp1con<5:4>. the pwm duty cycle is calculated by the following equation. equation 15-2: ccpr1l and ccp1con<5:4> can be written to at any time, but the duty cycle value is not copied into ccpr1h until a match between pr2 and tmr2 occurs (i.e., the period is complete). in pwm mode, ccpr1h is a read-only register. note: clearing the ccp1con register will force the ccp1 pwm output latch to the default low level. this is not the portc i/o data latch. ccpr1l ccpr1h (slave) comparator tmr2 comparator pr2 (note 1) rq s duty cycle registers ccp1con<5:4> clear timer, ccp1 pin and latch d.c. trisc<2> rc2/ccp1 note: 8-bit timer is concatenated with 2-bit internal q clock or 2 bits of the prescaler to create 10-bit time base. period duty cycle tmr2 = pr2 tmr2 = duty cycle tmr2 = pr2 note: the timer2 postscaler (see section 13.0 ?timer2 module? ) is not used in the deter- mination of the pwm frequency. the postscaler could be used to have a servo update rate at a different frequency than the pwm output. pwm period = [(pr2) + 1]  4  t osc  (tmr2 prescale value) pwm duty cycle = (ccpr1l:ccp1con<5:4>)  tosc  (tmr2 prescale value)
? 2003 microchip technology inc. preliminary ds39616b-page 157 pic18f2331/2431/4331/4431 the ccpr1h register and a 2-bit internal latch are used to double buffer the pwm duty cycle. this double buffering is essential for glitchless pwm operation. when the ccpr1h and 2-bit latch match tmr2, concatenated with an internal 2-bit q clock or two bits of the tmr2 prescaler, the ccp1 pin is cleared. the maximum pwm resolution (bits) for a given pwm frequency is given by the following equation. equation 15-3: 15.5.3 setup for pwm operation the following steps should be taken when configuring the ccp module for pwm operation: 1. set the pwm period by writing to the pr2 register. 2. set the pwm duty cycle by writing to the ccpr1l register and ccp1con<5:4> bits. 3. make the ccp1 pin an output by clearing the trisc<2> bit. 4. set the tmr2 prescale value and enable timer2 by writing to t2con. 5. configure the ccp1 module for pwm operation. table 15-3: example pwm frequencies and resolutions at 40 mhz table 15-4: registers associated with pwm and timer2 note: if the pwm duty cycle value is longer than the pwm period, the ccp1 pin will not be cleared. pwm resolution (max) = f osc f pwm log log(2) bits ? ? ? ? pwm frequency 2.44 khz 9.77 khz 39.06 khz 156.25 khz 312.50 khz 416.67 khz timer prescaler (1, 4, 16)1641111 pr2 value ffh ffh ffh 3fh 1fh 17h maximum resolution (bits) 10 10 10 8 7 6.58 name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 ? adif rcif txif sspif ccp1if tmr2if tmr1if -000 0000 -000 0000 pie1 ? adie rcie txie sspie ccp1ie tmr2ie tmr1ie -000 0000 -000 0000 ipr1 ? adip rcip txip sspip ccp1ip tmr2ip tmr1ip -111 1111 -111 1111 trisc portc data direction register 1111 1111 1111 1111 tmr2 timer2 module register 0000 0000 0000 0000 pr2 timer2 module period register 1111 1111 1111 1111 t2con ? toutps3 toutps2 toutps1 toutps0 tmr2on t2ckps1 t2ckps0 -000 0000 -000 0000 ccpr1l capture/compare/pwm register1 (lsb) xxxx xxxx uuuu uuuu ccpr1h capture/compare/pwm register1 (msb) xxxx xxxx uuuu uuuu ccp1con ? ? dc1b1 dc1b0 ccp1m3 ccp1m2 ccp1m1 ccp1m0 --00 0000 --00 0000 ccpr2l capture/compare/pwm register2 (lsb) xxxx xxxx uuuu uuuu ccpr2h capture/compare/pwm register2 (msb) xxxx xxxx uuuu uuuu ccp2con ? ? dc2b1 dc2b0 ccp2m3 ccp2m2 ccp2m1 ccp2m0 --00 0000 --00 0000 legend: x = unknown, u = unchanged, ? = unimplemented, read as ? 0 ?. shaded cells are not used by pwm and timer2.
pic18f2331/2431/4331/4431 ds39616b-page 158 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 159 pic18f2331/2431/4331/4431 16.0 motion feedback module the motion feedback module is a special-purpose peripheral designed for motion feedback applications. together with the power control pwm module (see section 17.0 ?power control pwm module? ), it pro- vides a variety of control solutions for a wide range of electric motors. the module actually consists of two hardware sub-modules:  input capture module (ic)  quadrature encoder interface (qei). together with timer5 (see section 14.0 ?timer5 mod- ule? ), these modules provide a number of options for motion and control applications. many of the features for the ic and qei submodules are fully programmable, creating a flexible peripheral structure that can accommodate a wide range of in-system uses. an overview of the available features is presented in table 16-1. a simplified block diagram of the entire motion feedback module is shown in figure 16-1. table 16-1: summary of motion feedback module features note: because the same input pins are common to the ic and qei submodules, only one of these two submodules may be used at any given time. if both modules are on, the qei submodule will take precedence. submodule mode(s) features timer function ic (3x)  synchronous  input capture  flexible input capture modes  available prescaler  selectable time base reset  special event trigger for adc sampling/conversion or optional tmr5 reset feature (cap1 only)  wake-up from sleep function  selectable interrupt frequency  optional noise filter tmr5  3x input capture (edge capture, pulse width, period measurement, capture on change)  special event triggers the a/d conversion on the cap1 input qei qei  detect position  detect direction of rotation  large bandwidth (fcy/16)  optional noise filter 16-bit position counter  position measurement  direction of rotation status velocity measurement  2x and 4x update modes  velocity event postscaler  counter overflow flag for low rotation speed  utilizes input capture 1 logic (ic1)  high and low velocity support tmr5  precise velocity measurement  direction of rotation status
pic18f2331/2431/4331/4431 ds39616b-page 160 preliminary ? 2003 microchip technology inc. figure 16-1: motion fee dback module block diagram t cy timer5 t5cki clock data bus<7:0> tmr5<15:0> cap1/indx cap2/qea cap3/qeb tmr5<15:0> t cy clock qeif special reset trigger ic3if tmr5if 8 8 8 8 ic2if ic1if 8 8 position counter qei control logic divider ic1 ic2 special event output direction prescaler prescaler prescaler velocity event postscaler ic3 filter filter filter special reset trigger qeb qea indx 3x input capture logic qei logic 8 tmr5 reset control timer reset timer reset chgif chgif ic3if ic2if qeif 8 qei mode decoder ic3drif ic2qeif filter
? 2003 microchip technology inc. preliminary ds39616b-page 161 pic18f2331/2431/4331/4431 16.1 input capture the input capture (ic) submodule implements the following features:  three channels of independent input capture (16-bits/channel) on the cap1, cap2 and cap3 pins  edge-trigger, period or pulse width measurement operating modes for each channel  programmable prescaler on every input capture channel  special event trigger output (ic1 only)  selectable noise filters on each capture input input channel (ic1) includes a special event trigger that can be configured for use in velocity measure- ment mode. its block diagram is shown in figure 16-2. ic2 and ic3 are similar, but lack the special event trig- ger features or additional velocity-measurement logic. a representative block diagram is shown in figure 16-3. please note that the time base is timer5. figure 16-2: input capture block diagram for ic1 cap1buf/velr (1) capxren and mode select prescaler 1, 4, 16 cap1 pin cap1m<3:0> q clocks 3 4 fltck<2:0> velm noise filter tmr5 timer5 reset timer reset control clock q clocks cap1m<3:0> mux first event cap1buf_clk reset 1 0 ic1_tr timer5 logic reset control reset velcap (2) clock/ reset/ interrupt decode logic special event reset ic1if note 1: cap1buf register is reconfigured as velr register when qei mode is active. 2: qei generated velocity pulses, vel_out, are downs ampled to produce this velocity capture signal.
pic18f2331/2431/4331/4431 ds39616b-page 162 preliminary ? 2003 microchip technology inc. figure 16-3: input capture block diagram for ic2 and ic3 capxbuf (1,2,3) capxren (2) and mode select prescaler 1, 4, 16 cap2/cap3 pin capxm<3:0> (1) q?s 3 4 fltck<2:0> noise filter tmr5 enable tmr5 tmr5 reset timer reset control capture clock icxif (1) capture clock/ q clocks capxm<3:0> (1) capxbuf_clk (1) reset reset/ interrupt decode logic note 1: ic2 and ic3 are denoted as x=2 and 3. 2: cap2buf is enabled as poscnt when qei mode is active. 3: cap3buf is enabled as maxcnt when qei mode is active.
? 2003 microchip technology inc. preliminary ds39616b-page 163 pic18f2331/2431/4331/4431 the three input capture channels are controlled through the input capture control registers cap1con, cap2con, and cap3con. each channel is configured independently with its dedicated register. the implementation of the registers is identical, except for the special event trigger (see section 16.1.8 ?spe- cial event trigger (cap1 only)? ). the typical capture control register is shown in register 16-1. register 16-1: capxcon: input capture control register when in counter mode, the counter must be configured as the synchronous counter only (tmr5sync = 0 ). when configured in asynchronous mode, the ic module will not work properly. u-0 r/w-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? capxren ? ? capxm3 capxm2 capxm1 capxm0 bit 7 bit 0 bit 7 unimplemented: read as ? 0 ? bit 6 capxren: time base reset enable bit 1 = enabled 0 = disable selected time base reset on capture. bit 5 unimplemented: read as ? 0 ? bit 4 unimplemented: read as ? 0 ? bit 3-0 capxm3:capxm0: input capture 1 (icx) mode select bits 1111 = special event trigger mode. the trigger occurs on every rising edge on cap1 input (1) 1110 = special event trigger mode. the trigger occurs on every falling edge on cap1 input (1) 1101 = unused 1100 = unused 1011 = unused 1010 = unused 1001 = unused 1000 = capture on every capx input state change 0111 = pulse width measurement mode, every rising to falling edge 0110 = pulse width measurement mode, every falling to rising edge 0101 = frequency measurement mode, every rising edge 0100 = capture mode, every 16th rising edge 0011 = capture mode, every 4th rising edge 0010 = capture mode, every rising edge 0001 = capture mode, every falling edge 0000 = input capture 1 (icx) off note 1: special event trigger is only available on cap1. for cap2 and cap3, this config- uration is unused. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown note: throughout this section, references to registers and bit names that may be asso- ciated with a specific capture channel will be referred to generically by the use of the term ?x? in place of the channel number. for example, ?capxren? may refer to the capture reset enable bit in cap1con, cap2con or cap3con.
pic18f2331/2431/4331/4431 ds39616b-page 164 preliminary ? 2003 microchip technology inc. 16.1.1 edge capture mode in this mode, the value of the time base is captured either on every rising edge, every falling edge, every 4th rising edge, or every 16th rising edge. the edge present on the input capture pin (cap1, cap2 or cap3) is sampled by the synchronizing latch. the signal is used to load the input capture buffer (icxbuf register) on the following q1 clock (see figure 16-4). consequently, timer5 is either reset to ? 0 ? (q1 immediately following the capture event) or left free running, depending on the setting of capture reset enable, capxren, in the capxcon register. figure 16-4: edge capture mode timing note 1: input capture prescalers are reset (cleared) when the input capture module is disabled (capxm = 0000 ). 2: when the input capture mode is changed without first disabling the module and entering the new input capture mode, a false interrupt (or special event trigger on ic1) may be generated. the user should either (1) disable the input capture before entering another mode or (2) disable ic interrupts to avoid false interrupts during ic mode changes. 3: during ic mode changes, the prescaler count will not be cleared, therefore the first capture in the new ic mode may be from the non-zero prescaler. note: on the first capture edge following the setting of the input capture mode (i.e., movwf cap1con), timer5 contents are always captured into the corresponding input capture buffer (i.e., capxbuf). timer5 can optionally be reset; however, this is dependent on the setting of the capture reset enable bit (capxren), see figure 16-4. cap1 pin (2) 0000 0002 0000 0001 0002 tmr5 (1) cap1buf (3) 0002 0014 0013 0012 0015 abcd 0001 0003 tmr5 reset (4) movwf cap1con bcf cap1con, cap1ren note 5 0016 instruction note 1: tmr5 is a synchronous time base input to the input capt ure, prescaler = 1:1. it increments on q1 rising edge. 2: ic1 is configured in edge capture mode (cap1m3:cap1m0 = 0010 ) with the time base reset upon edge capture (cap1ren = 1 ) and no noise filter. 3: tmr5 value is latched by cap1buf on t cy . in the event that a write to tmr5 coincides with an input capture event , the write will always take precedence. all input capture buffers, cap1buf, cap2buf and cap3buf, are updated with the incremented value of the time base on the next t cy clock edge when the capture event takes place (see note 4 when reset occurs). 4: tmr5 reset is normally an asynchronous reset signal to tmr5 . when used with the input capt ure, it is active immedi - ately after the time base value is captured. 5: tmr5 reset pulse is disabled by clearing cap1ren bit (e.g, bcf cap1con , cap1ren ). osc execution q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4
? 2003 microchip technology inc. preliminary ds39616b-page 165 pic18f2331/2431/4331/4431 16.1.2 period measurement mode the period measurement mode is selected by setting capxm3:capxm0 = 0101 . in this mode, the value of timer5 is latched into the capxbuf register on the ris- ing edge of the input capture trigger and timer5 is sub- sequently reset to 0000h (optional by setting capxren = 1 ) on the next t cy (see capture and reset relationship in figure 16-4). 16.1.3 pulse width measurement mode the pulse width measurement mode can be config- ured for two different edge sequences, such that the pulse width is based on either the falling to rising edge of the capx input pin (capxm3:capxm0 = 0110 ), or on the rising to falling edge (capxm3:capxm0 = 0111 ). timer5 is always reset on the edge when the measurement is first initiated. for example, when the measurement is based on the falling to rising edge, timer5 is first reset on the falling edge and the timer value is captured on the rising edge thereafter. upon entry into the pulse width measurement mode, the very first edge detected on the capx pin is always captured. the tmr5 value is reset on the first active edge (see figure 16-5). figure 16-5: pulse width measurement mode timing cap1 pin (2) 0000 0002 0000 0001 0002 tmr5 (1) cap1buf (3) 0002 0014 0013 0012 0015 0015 0001 tmr5 reset (4,5) movwf cap1con instruction 0001 note 1: tmr5 is a synchronous time base input to the input captur e, prescaler = 1:1. it increments on every q1 rising edge. 2: ic1 is configured in pulse width measurement mode (cap1m3:cap1m0 = 0111 , rising to falling pulse width measure- ment). no noise filter on cap1 input is used. movwf instruction loads cap1con when w = 0111 . 3: tmr5 value is latched by cap1buf on t cy rising edge. in the event that a write to tmr5 coincides with an input cap- ture event, the write will always take precedence. all input capture buffers, cap1buf, cap2buf and cap3buf, are updated with the incremented value of the time base on the next t cy clock edge when the capture event takes place (see note 4 when reset occurs). 4: tmr5 reset is normally an asynchronous reset signal to tmr5. when used in pulse width measurement mode, it is always present on the edge that first initiates the pulse wid th measurement (i.e., when configured in the rising to falling pulse width measurement mode, it is ac tive on each rising edge detected. in the falling to rising pulse width measure- ment mode, it is active on each falling edge detected. 5: tmr5 reset pulse is activated on the capture edge. cap1ren bit has no bearing in this mode. execution (2) q1q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1q2 q3 q4
pic18f2331/2431/4331/4431 ds39616b-page 166 preliminary ? 2003 microchip technology inc. 16.1.3.1 pulse width measurement timing pulse width measurement accuracy can be only ensured when the pulse width high and low present on capx input exceeds one t cy clock cycle. the limitations depend on the mode selected:  when capxm3:capxm0 = 0110 (rising-to-falling edge delay), the capx input high pulse width (tcch) must exceed t cy + 10 ns.  when capxm3:capxm0 = 0111 (falling-to-rising edge delay), the capx input low pulse width (tccl) must exceed t cy + 10 ns. 16.1.4 input capture on state change when capxm3:capxm0 = 1000 , the value is captured on every signal change on the capx input. if all three capture channels are configured in this mode, the three-input-capture can be used as the hall-effect sensor state transition detector. the value of timer5 can be captured, timer5 reset and the interrupt generated. any change on cap1, cap2 or cap3 is detected and the associated time base count is captured. for position and velocity measurement in this mode, the timer can be optionally reset (see section 16.1.6 ?timer5 reset? for reset options). figure 16-6: input capture on state c hange (hall-effect sensor mode) note 1: the period measurement mode will produce valid results upon sampling of the second rising edge of the input capture. capxbuf values latched during the first active edge after initialization are invalid. 2: the pulse width measurement mode will latch the value of the timer upon sampling of the first input signal edge by the input capture. cap2 cap1 cap3 time base (1) cap1buf (2) cap2buf (2) cap3buf (2) 0fffh 0000h time base reset (1) 1 0 1 1 0 0 1 1 0 0 1 0 0 1 1 0 0 1 state 1 state 2 state 3 state 4 state 5 state 6 note 1: tmr5 can be selected as the time base for input capture. time base can be optionally reset when the capture reset enabled bit is set (capxren = 1 ). 2: detailed capxbuf event timing (all modes reflect same capt ure and reset timing) is shown in figure 16-4.there are six commutation bldc hall-effect sensor states show n. the other two remaining states (i.e., 000h and 111h ) are invalid in the normal operation. they are still to be decoded by the cpu firmware in bldc motor application.
? 2003 microchip technology inc. preliminary ds39616b-page 167 pic18f2331/2431/4331/4431 16.1.5 entering input capture mode and capture timing the following is a summary of functional operation upon entering any of the input capture modes: 1. after the module is configured for one of the capture modes by setting the mode select bits (capxm3:capxm0), the first detected edge captures timer5 value and stores it in the capx- buf register. the timer is then reset (depending on the setting of capxren bit) and starts to increment according to its settings, see figure 16-4, figure 16-5 and figure 16-6. 2. on all edges, the capture logic performs the fol- lowing: a) input capture mode is decoded and the active edge is identified b) the capxren bit is checked to determine whether timer5 is reset or not. c) on every active edge, the timer5 value is recorded in the input capture buffer (capx- buf). d) reset timer5 after capturing the value of the timer when capxren bit is enabled. timer5 is reset on every active capture edge in this case. e) on all continuing capture edge events repeat steps 1 through 4 until the opera- tional mode is terminated either by user firmware, por or bor. f) the timer value is not affected when switch- ing into and out of various input capture modes. 16.1.6 timer5 reset every input capture trigger can optionally reset (tmr5). capture reset enable bit, capxren, gates the automatic reset of the time base of the capture event with this enable reset signal. all capture events reset the selected timer when capxren is set. resets are disabled when capxren is cleared (see figure 16-4, figure 16-5 and figure 16-6). 16.1.7 ic interrupts there are four operating modes for which the ic module can generate an interrupt and set one of the interrupt capture flag bits (ic1if, ic2qeif or ic3drif). the interrupt flag that is set depends on the channel in which the event occurs. the modes are:  edge capture (capxm3:capxm0 = 0001 , 0010 , 0011 or 0100 )  period measurement event (capxm3:capxm0 = 0101 )  pulse width measurement event (capxm3:capxm0 = 0110 or 0111 )  state change event (capxm3:capxm0 = 1000 ) the timing of interrupt and special trigger events is shown in figure 16-7. any active edge is detected on the rising edge of q2 and propagated on the rising edge of q4 rising edge. if an active edge happens to occur any later than this (on the falling edge of q2, for example), then it will be recognized on the next q2 rising edge. note: the capxren bit has no effect in pulse width measurement mode. note: the special event trigger is generated only in the special event trigger mode on cap1 input (cap1m2:cap1m0> = 1110 and 1111 ). ic1if interrupt is not set in this mode.
pic18f2331/2431/4331/4431 ds39616b-page 168 preliminary ? 2003 microchip technology inc. figure 16-7: capxif interrupts and ic1 special event trigger 16.1.8 special event trigger (cap1 only) the special event trigger mode of ic1 (cap1m3:cap1m0 = 1110 or 1111 ) enables the special event trigger signal. the trigger signal can be used as the special event reset input to tmr5, resetting the timer when the specific event happens on ic1. the events are summarized in table 16-2. table 16-2: special event trigger 16.1.9 operating modes summary table 16-3 shows a summary of the input capture con- figuration when used in conjunction with tmr5 timer resource. 16.1.10 other operating modes although the ic and qei submodules are mutually exclusive, the ic can be reconfigured to work with the qei module to perform specific functions. in effect, the qei ?borrows? hardware from the ic to perform these operations. for velocity measurement, the qei uses dedicated hardware in channel ic1. the cap1buf registers are remapped, becoming the vreg registers. its operation and use are described in section 16.2.6 ?velocity measurement? . while in qei mode, the cap2buf and cap3buf reg- isters of channel ic2 and ic3 are used for position determination. they are remapped as the poscnt and maxcnt buffer registers, respectively. q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 osc cap1 pin ic1if tmr5 tmr5 reset xxxx 0000 0001 tmr5on (1) note 1: timer5 is only reset and enabled (assuming: tmr5on = 0 and tmr5mod = 1 ) when the special event reset trigger is enabled for the timer5 reset input. tmr5on bit is asserted and timer5 is reset on the q1 rising edge following the event capture. with the special event reset trigger disabled, timer5 cannot be reset by the special event rese t trigger on cap1 input. in order for the special event reset tr igger to work as the reset trigger to timer5, ic1 must be configured in the special even t trigger mode (cap1m<3:0> = 1110 or 1111 ). cap1m3: cap1m0 description 1110 the trigger occurs on every falling edge on cap1 input 1111 the trigger occurs on every rising edge on cap1 input
? 2003 microchip technology inc. preliminary ds39616b-page 169 pic18f2331/2431/4331/4431 table 16-3: input capture time base reset summary pin capxm mode timer reset timer on capture description cap1 0001-0100 edge capture tmr5 optional (1) simple edge capture mode (includes a selectable prescaler) 0101 period measurement tmr5 optional (1) captures timer5 on period boundaries 0110-0111 pulse width measurement tmr5 always captures timer5 on pulse boundaries 1000 input capture on state change tmr5 optional (1) captures timer5 on state change 1110-1111 special event trigger (rising or falling edge) tmr5 optional (2) used as a special event trigger to be used with the timer5 or other peripheral modules cap2 0001-0100 edge capture tmr5 optional (1) simple edge capture mode (includes a selectable prescaler 0101 period measurement tmr5 optional (1) captures timer5 on period boundaries 0110-0111 pulse width measurement tmr5 always captures timer5 on pulse boundaries 1000 input capture on state change tmr5 optional (1) captures timer5 on state change cap3 0001-0100 edge capture tmr5 optional (1) simple edge capture mode (includes a selectable prescaler 0101 period measurement tmr5 optional (1) captures timer5 on period boundaries 0110-0111 pulse width measurement tmr5 always captures timer5 on pulse boundaries 1000 input capture on state change tmr5 optional (1) captures timer5 on state change note 1: timer5 may be reset on capture events only when capxre = 1 . 2: trigger mode will not reset timer5 unless resen = 0 in the t5con register.
pic18f2331/2431/4331/4431 ds39616b-page 170 preliminary ? 2003 microchip technology inc. 16.2 quadrature encoder interface the quadrature encoder interface (qei) decodes speed and motion sensor information. it can be used in any application that uses a quadrature encoder for feedback. the interface implements these features:  three qei inputs: two phase signals (qea and qeb) and one index signal (indx)  direction of movement detection with a direction change interrupt (ic3drif)  16-bit up/down position counter  standard and high-precision position tracking modes  two position update modes (x2 and x4)  velocity measurement with a programmable postscaler for high-speed velocity measurement  position counter interrupt (ic2qeif in the pir3 register)  velocity control interrupt (ic1if in the pir3 register) the qei sub-module has three main components: the qei control logic block, the position counter and velocity postscaler. the qei control logic detects the leading edge on the qea or qeb phase input pins, and generates the count pulse which is sent to the position counter logic. it also samples the index input signal (indx), and generates the direction of rotation signal (up/down) and the veloc- ity event signals. the position counter acts as an integrator for tracking distance traveled. the qea and qeb input edges serve as the stimulus to create the input clock which advances the position counter register (poscnt). the register is incremented on either the qea input edge, or the qea and qeb input edges, depending on the operating mode. it is reset either by a rollover on match to the period register, maxcnt, or on the exter- nal index pulse input signal (indx). an interrupt is gen- erated on a reset of poscnt if the position counter interrupt is enabled. the velocity postscaler down-samples the velocity pulses used to increment the velocity counter by a specified ratio. it essentially divides down the number of velocity pulses to one output per so many input, pre- serving the pulse width in the process. a simplified block-diagram of the qei module is shown in figure 16-8. figure 16-8: qei block diagram comparator cap2/qea cap1/indx cap3/qeb poscnt/cap2buf reset on match qei module qei control logic filter filter filter qeb qea indx timer reset velocity event postscaler 8 maxcnt/cap3buf 8 8 clock direction 8 8 set ic2qeif set up/down data bus position counter reset timer5 direction change set chgif velocity capture
? 2003 microchip technology inc. preliminary ds39616b-page 171 pic18f2331/2431/4331/4431 16.2.1 qei configuration the qei module shares its input pins with the input capture module. the inputs are mutually exclusive; only the ic module or the qei module (but not both) can be enabled at one time. also, because the ic and qei are multiplexed to the same input pins, the programmable noise filters can be dedicated to one module only. the operation of the qei is controlled by the qeicon configuration register. see register 16-2. register 16-2: qeicon: quadrature encoder interface control register note: in the event that both qei and ic are enabled, qei will take precedence and ic will remain disabled. r/w-0 r/w-0 r-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 velm error up/down qeim2 qeim1 qeim0 pdec1 pdec0 bit 7 bit 0 bit 7 velm : velocity mode bit 1 = velocity mode disabled 0 = velocity mode enabled bit 6 error: qei error bit (1) 1 = position counter (4) overflow or underflow 0 = no overflow or underflow bit 5 up/down : direction of rotation status bit (1) 1 =forward 0 = reverse bit 4-2 qeim2:qeim0: qei mode bits (2,3) 111 =unused 110 =qei enabled in 4x update mode; position counter reset on period match (poscnt = maxcnt) 101 =qei enabled in 4x update mode; indx resets the position counter 100 =unused 010 =qei enabled in 2x update mode; position counter reset on period match (poscnt = maxcnt) 001 =qei enabled in 2x update mode; indx resets the position counter 000 =qei off bit 1-0 pdec1:pdec0: velocity pulse reduction ratio bit 11 =1:64 10 =1:16 01 =1:4 00 =1:1 note 1: qei must be enabled and in index mode. 2: qei mode select must be cleared (= 000 ) to enable cap1, cap2 or cap3 inputs. if qei and ic modules are both enabled, qei will take precedence. 3: enabling one of the qei operating modes remaps the ic buffer registers cap1bufh, cap1bufl, cap2bufh, cap2bufl, cap3 bufh and cap3bufl as the vregh, vregl, poscnth, poscntl, maxcnth, and maxcntl registers (respectively) for the qei. 4: error bit must be cleared in software. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 172 preliminary ? 2003 microchip technology inc. 16.2.2 qei modes position measurement resolution depends on how often the position counter register, poscnt, is incremented. there are two qei update modes to measure the rotor?s position: qei x2 and qei x4. table 16-4: qei modes 16.2.2.1 qei x2 update mode qei x2 update mode is selected by setting the qei mode select bits (qeim2:qeim0) to ? 001 ? or ? 010 ?. in this mode, the qei logic detects every edge on the qea input only. every rising and falling edge on the qea signal clocks the position counter. the position counter can be reset by either an input on the indx pin (qeim2:qeim0 = 001 ), or by a period-match, even when the poscnt register pair equals maxcnt (qeim2:qeim0 = 010 ). 16.2.2.2 qei 4x update mode qei x4 update mode provides for a finer resolution of the rotor position, since the counter increments or decrements more frequently for each qea/qeb input pulse pair than in qei x2 mode. this mode is selected by setting the qei mode select bits to 101 or 110 . in qei x4, the phase measurement is made on the rising and the falling edges of both qea and qeb inputs. the position counter is clocked on every qea and qeb edge. like qei x2 mode, the position counter can be reset by an input on the pin (qeim2:qeim0 = 101 ), or by the period-match event (qeim2:qeim0 = 010 ). 16.2.3 qei operation the position counter register pair (poscnth: poscntl) acts as an integrator, whose value is pro- portional to the position of the sensor rotor that corre- sponds to the number of active edges detected. poscnt can either increment or decrement, depend- ing on a number of selectable factors which are decoded by the qei logic block. these include the count mode selected, the phase relationship of qea to qeb (?lead/lag?), the direction of rotation, and if a reset event occurs. the logic is detailed in the sections that follow. 16.2.3.1 edge and phase detect in the first step, the active edges of qea and qeb are detected, and the phase relationship between them is determined. the position counter is changed based on the selected qei mode. in qei x2 update mode, the position counter incre- ments or decrements on every qea edge based on the phase relationship of the qea and qeb signals. in qei x4 update mode, the position counter increments or decrements on every qea and qeb edge based on the phase relationship of the qea and qeb signals. for example, if qea leads qeb, the position counter is incremented by 1. if qeb lags qea, the position counter is decremented by 1. 16.2.3.2 direction of count the qei control logic generates a signal that sets the up/down bit (qeicon<5>); this in turn determines the direction of the count. when qea leads qeb, up/down is set (= 1 ), and the position counter increments on every active edge. when qea lags qeb, up/down is cleared, and the position counter decrements on every active edge. table 16-5: direction of rotation qeim2: qeim0 mode/ reset description 000 ? qei disabled (1) 001 x2 update/ index pulse two clocks per qea pulse. indx resets poscnt. 010 x2 update/ period match two clocks per qea pulse. poscnt reset by the period match (maxcnt). 011 ? unused 100 ? unused 101 x4 update/ index four clocks per qea and qeb pulse pair. indx resets poscnt. 110 x4 update/ period match four clocks per qea and qeb pulse pair. poscnt reset by the period match (maxcnt). 111 ? unused note 1: qei module is disabled. the position counter and the velocity measurement functions are fully disabled in this mode. current signal detected previous signal detected pos. cntrl. (1) rising falling qea qeb qea qeb qea rising x inc xdec qea falling x dec xinc qeb rising x inc xdec qeb falling x inc xdec note 1: when up/down = 1 , the position counter is incremented; when up/down = 0 , the position counter is decremented.
? 2003 microchip technology inc. preliminary ds39616b-page 173 pic18f2331/2431/4331/4431 16.2.3.3 reset and update events the position counter will continue to increment or dec- rement until one of the following events takes place. the type of event and the direction of rotation when it happens determines if a register reset or update occurs. 1. an index pulse is detected on the indx input (qeim2:qeim0 = 001 ). if the encoder is traveling in the forward direc- tion, poscnt is reset (00h) on the next clock edge after the index marker, indx, has been detected. the position counter resets on the qea or qeb edge once the indx rising edge has been detected. if the encoder is traveling in the reverse direc- tion, the value in the maxcnt register is loaded into poscnt on the next quadrature pulse edge (qea or qeb) after the falling edge on indx has been detected. 2. a postcnt/maxcnt period match occurs (qeim2:qeim0 = 010 ). if the encoder is traveling in the forward direc- tion, poscnt is reset (00h) on the next clock edge when poscnt = maxcnt. an interrupt event is triggered on the next t cy after the reset (see figure 16-10) if the encoder is traveling in the reverse direction and the value of poscnt reaches 00h, poscnt is loaded with the contents of maxcnt register on the next clock edge. an interrupt event is triggered on the next t cy after the load operation (see figure 16-10). the value of the position counter is not affected during qei mode changes, nor when the qei is disabled altogether. 16.2.4 qei interrupts the position counter interrupt occurs, and the interrupt flag (ic2qeif) is set, based on the following events:  a poscnt/maxcnt period match event (qeim2:qeim0 = 010 or 110 )  a poscnt rollover (ffffh to 0000h) in period mode only (qeim2:qeim0 = 010 or 110 )  an index pulse detected on indx. the interrupt timing diagrams for ic2qeif are shown in figure 16-10 and figure 16-11. when the direction has changed, the direction change interrupt flag (ic3drif) is set on the following t cy clock (see figure 16-10). if the position counter rolls over in index mode, the error bit will be set. 16.2.5 qei sample timing the quadrature input signals, qea and qeb, may vary in quadrature frequency. the minimum quadrature input period t qei is 16t cy . the position count rate, f pos , is directly proportional to the rotor?s rpm, line count d and qei update mode (x2 vs. x4); that is, the maximum position count rate (i.e., 4x qei update mode, d = 1024) with f cy = 10 mips is equal to 2.5 mhz, which corresponds to f qei of 625 khz. figure 16-9 shows qea and qeb quadrature inputs timing when sampled by the noise filter. note: the number of incremental lines in the position encoder is typically set at d = 1024 and the qei update mode = x4. f pos 4 d rpm ? 60 --------------------- --- =
pic18f2331/2431/4331/4431 ds39616b-page 174 preliminary ? 2003 microchip technology inc. figure 16-9: qei inputs when sampled by the filter (divide ratio = 1:1) figure 16-10: qei module reset timing on period match t cy qea pin t gd = 3t cy qeb pin qea input qeb input t qei = 16t cy (1) note 1: the module design allows a quadrature frequency of up to f qei = f cy /16. qeb qea up/down ic2qeif poscnt (1) 1520 1521 1522 1523 1524 1525 1526 1527 count (+/-) +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 maxcnt maxcnt=1527 0000 0001 0002 0003 0004 0003 0002 0001 0000 1527 1526 1525 1524 1523 1522 1521 1520 1519 1518 1517 1516 1515 1514 q4 (3) q4 (3) q1 (4) q1 (5) position counter load forward reverse q1 ( 5) ic3drif note 2 note 2 note 1: poscnt register is shown in qei x4 update mode (p oscnt increments on every rising and every falling edge of qea and qeb input signals). asynchronous external qea and qeb input are synchronized to t cy clock by the input sampling ff in the noise filter (see figure 16-14). 2: when poscnt = maxcnt, poscnt is reset to ? 0 ? on the next qea rising edge. poscnt is set to maxcnt when poscnt = 0 (when decrementing), which occurs on the next qea falling edge. 3: ic2qei is generated on q4 rising edge. 4: position counter is loaded with ? 0 ? (which is a rollover event in this case) on poscnt = maxcnt. 5: position counter is loaded with maxcnt value (1527h) on underflow. 6: ic2qeif must be cleared in software. note 6
? 2003 microchip technology inc. preliminary ds39616b-page 175 pic18f2331/2431/4331/4431 figure 16-11: qei module reset timing with the index input 16.2.6 velocity measurement the velocity pulse generator, in conjunction with the ic1 and the synchronous tmr5 (in synchronous operation), provides a method for high accuracy speed measurements at both low and high mechanical motor speeds. the velocity mode is enabled when the velm bit is cleared (= 0 ) and qei is set to one of its operating modes (see table 16-6). to optimize register space, the input capture channel one (ic1) is used to capture tmr5 counter values. input capture buffer register, cap1buf, is redefined in velocity measurement mode, velm = 0 , as the velocity register buffer (vregh, vregl). table 16-6: velocity pulses qeb qea up/down ic2qeif poscnt (1) 1520 1521 1522 1523 1524 1525 1526 1527 count (+/-) +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 maxcnt maxcnt=1527 0000 0001 0002 0003 0004 0003 0002 0001 0000 1527 1526 1525 1524 1523 1522 1521 1520 1519 1518 1517 1516 1515 1514 q4 (3) q4 (3) indx q1 (4) q1 (5) position counter load forward reverse note 2 note 2 note 1: poscnt register is shown in qei x4 update mode (p oscnt increments on every rising and every falling edge of qea and qeb input signals) 2: when indx reset pulse is detected, poscnt is reset to ? 0 ? on the next qea or qeb edge. poscnt is set to maxcnt when poscnt = 0 (when decrementing), which occurs on t he next qea or qeb edge. similar reset sequence occurs for the reverse direction except that the indx signal is recognized on its falling edge. the reset is generated on the next qea or qeb edge. 3: ic2qei is enabled for one t cy clock cycle. 4: position counter is loaded with ?0000h? (i.e., reset) on the next qea or qeb edge when indx is high. 5: position counter is loaded with maxcnt value (e.g., 1527h) on the next qea or qeb edge following the indx falling edge input signal detect). 6: ic2qeif must be cleared in software. note 6 qeim<2:0> velocity event mode 001 010 x2 velocity event mode. the velocity pulse is generated on every qea edge. 101 110 x4 velocity event mode. the velocity pulse is generated on every qea and qeb active edge.
pic18f2331/2431/4331/4431 ds39616b-page 176 preliminary ? 2003 microchip technology inc. 16.2.6.1 velocity event timing the event pulses are reduced by a fixed ratio by the velocity pulse divider. the divider is useful for high-speed measurements where the velocity events happen frequently. by producing a single output pulse for a given number of input event pulses, the counter can track larger pulse counts (i.e., distance travelled) for a given time interval. time is measured by utilizing the tmr5 time base. each velocity pulse serves as a capture pulse. with the tmr5 in synchronous timer mode, the value of tmr5 is captured on every output pulse of the postscaler. the counter is subsequently reset to ? 0 ?. tmr5 is reset upon a capture event. figure 16-13 shows the velocity measurement timing diagram. figure 16-12: velocity measurement block diagram cap2/qea cap1/indx cap3/qeb qei control logic qeb qea indx postscaler clock direction position counter tmr5 ic1 noise filters reset logic 16 velocity mode (velr register) t cy clock tmr5 reset velocity capture velocity event
? 2003 microchip technology inc. preliminary ds39616b-page 177 pic18f2331/2431/4331/4431 figure 16-13: velocity measurement timing (1) 16.2.6.2 velocity postscaler the velocity event pulse (velcap, see figure 16-12) serves as the tmr5 capture trigger to ic1 while in the velocity mode. the number of velocity events are reduced by the velocity postscaler before they are used as the input capture clock. the velocity event reduction ratio can be set with the pdec1:pdec0 control bits (qeicon<1:0>) to 1:4, 1:16, 1:64 or no reduction (1:1). the velocity postscaler settings are automatically reloaded from their previous values as the velocity mode is re-enabled. 16.2.6.3 cap1ren in velocity mode the tmr5 value can be reset (tmr5 register pair = 0000h) on a velocity event capture by setting the cap1ren bit (cap1con<6>). when cap1ren is cleared, the tmr5 time base will not be reset on any velocity event capture pulse. the velr register pair, however, will continue to be updated with the current tmr5 value. qeb qea cnt_reset (3) tmr5 (2) velcap bcf tmr5con, velm velr (2) 1520 1521 1522 1523 1525 1526 1527 1528 1529 1530 1531 1532 1534 1535 1536 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0000 0001 0002 0003 0004 1529 1524 1533 1537 vel_out old value forward q1 q1 ic1if (4) q1 instr. execution movwf qeicon (5) bcf pie2, ic1ie bsf pie2, ic1ie cap1ren note 1: timing shown is for qeim<2:0> = 101 , 110 or 111 (x4 update mode enabled) and the velocity postscaler divide ratio is set to divide by 4 (pdec<1:0> = 01 ). 2: velr register latches the tmr5 count on the ?velcap? capture pulse. timer5 must be set to the synchronous timer or counter mode. in this example, it is set to the sync hronous timer mode where the tmr5 prescaler divide ratio = 1 (i.e., timer5 clock = t cy ). 3: the tmr5 counter is reset on the next q1 clock cycle following the ?velcap? pulse. tmr5 value is unaffected when the velocity measurement mode is first enabled (velm = 0 ). the velocity postscaler values must be reconfigured to their previous settings when re-entering velocity measurement mode. while making speed measurements of very slow rotational speeds (e.g., servo-controller applications), the velocity measurement mode may not provide sufficient precision. the pulse width measurement mode may have to be used to provide t he additional precision. in this case, the input pulse is measured on the cap1 input pin. 4: ic1if interrupt is enabled by setting ic1ie as follows, bsf pie2, ic1ie . assume ic1e bit is placed in pie2 peripheral interrupt enable register in the target device. the actual ic1if bit is written on q2 rising edge. 5: post decimation value is changed from pdec = 01 (decimate by 4) to pdec = 00 (decimate by 1). reverse
pic18f2331/2431/4331/4431 ds39616b-page 178 preliminary ? 2003 microchip technology inc. 16.3 noise filters the motion feedback module includes three noise rejection filters on cap1/indx, cap2/qea and cap3/qeb. the filter block also includes a fourth filter for the t5cki pin. they are intended to help reduce spurious noise spikes which may cause the input sig- nals to become corrupted at the inputs. the filter ensures that the input signals are not permitted to change until a stable value has been registered for three consecutive sampling clock cycles. the filters are controlled using the digital filter control (dfltcon) register (see register 16-3). the filters can be individually enabled or disabled by setting or clearing the corresponding fltxen bit in the dfltcon register. the sampling frequency, which must be the same for all three noise filters, can be programmed by the fltck2:fltck0 configuration bits. t cy is used as the clock reference to the clock divider block. the noise filters can either be added or removed from the input capture or qei signal path by setting or clearing the appropriate fltxen bit, respectively. each capture channel provides for individual enable control of the filter output. the flt4en bit enables or disabled the noise filter available on tmr5cki input in the timer5 module. the filter network for all channels is disabled on por and bor resets , as the dfltcon register is cleared on resets. the operation of the filter is shown in the timing diagram in figure 16-14. register 16-3: dfltcon: digital filter control register u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? flt4en flt3en flt2en flt1en fltck2 fltck1 fltck0 bit 7 bit 0 bit 7 unimplemented: read as ? 0 ? bit 6 flt4en: noise filter output enable bit, t5cki input 1 = enabled 0 = disabled bit 5 flt3en: noise filter output enable bit, cap3/qeb input (1) 1 = enabled 0 = disabled bit 4 flt2en: noise filter output enable bit, cap2/qea input (1) 1 = enabled 0 = disabled bit 3 flt1en: noise filter output enable bit, cap1/indx input (1) 1 = enabled 0 = disabled bit 2-0 fltck<2:0>: noise filter clock divider ratio bits 111 =unused 110 =1:128 101 =1:64 100 =1:32 011 =1:16 010 =1:4 001 =1:2 000 =1:1 note 1: noise filter output enables are functional in both qei and ic operating modes legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown note: the noise filter is intended for random high-frequency filtering and not continuous high-frequency filtering.
? 2003 microchip technology inc. preliminary ds39616b-page 179 pic18f2331/2431/4331/4431 figure 16-14: filter timing di agram (clock divider = 1:1) 16.4 ic and qei shared interrupts the ic and qei sub-modules can each generate three distinct interrupt signals; however, they share the use of the same three interrupt flags in register pir3. the meaning of a particular interrupt flag at any given time depends on which module is active at the time the interrupt is set. the meaning of the flags in context are summarized in table 16-7. when the ic submodule is active, the three flags (ic1if, ic2qeif and ic3drif) function as interrupt-on-capture event flags for their respective input capture channels. the channel must be configured for one of the events that will generate an interrupt (see section 16.1.7 ?ic interrupts? for more information). when the qei is enabled, the ic1if interrupt flag indicates an interrupt caused by a velocity measurement event, usually an update of the velr register. the ic2qeif interrupt indicates that a position measurement event has occurred. ic3drif indicates that a direction change has been detected. table 16-7: meaning of ic and qei interrupt flags 16.5 operation in sleep mode 16.5.1 3x input capture in sleep mode since the input capture can operate only when its time base is configured in a synchronous mode, the input capture will not capture any events. this is because the device?s internal clock has been stopped, and any internal timers in synchronous modes will not incre- ment. the prescaler will continue to count the events (not synchronized). when the specified capture event occurs, the capxif interrupt will be set. the capture buffer register will be updated upon wake-up from sleep to the current tmr5 value. if the capxif interrupt is enabled, the device will wake-up from sleep. this effectively enables all input capture channels to be used as the external interrupts. 16.5.2 qei in sleep mode all qei functions are halted in sleep mode. cap1/indx pin (1) t cy noise glitch (3) cap1/indx input (2) t gd = 3t cy noise glitch (3) t qei = 16t cy note 1: only cap1/indx pin input is shown for simplicity. simi lar event timing occurs on cap2/qea and cap3/qeb pins. 2: noise filtering occurs in shaded portions of cap1 input. 3: filter?s group delay: t gd = 3 t cy . (input to filter) (output from filter) interrupt flag meaning ic mode qei mode ic1if ic1 capture event velocity register update ic2qeif ic2 capture event position measurement update ic3drif ic3 capture event direction change
pic18f2331/2431/4331/4431 ds39616b-page 180 preliminary ? 2003 microchip technology inc. table 16-8: registers associated with the motion feedback module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u ipr3 ? ? ? ptip ic3drip ic2qeip ic1ip tmr5ip ---1 1111 ---1 1111 pie3 ? ? ? ptie ic3drie ic2qeie ic1ie tmr5ie ---0 0000 ---0 0000 pir3 ? ? ? ptif ic3drif ic2qeif ic1if tmr5if ---0 0000 ---0 0000 tmr5h timer5 register high byte (buffer) xxxx xxxx uuuu uuuu tmr5l timer5 register low byte xxxx xxxx uuuu uuuu pr5h timer5 period register high byte 1111 1111 1111 1111 pr5l timer5 period register low byte 1111 1111 1111 1111 t5con t5sen resen t5mod t5ps1 t5ps0 t5sync tmr5cs tmr5on 0000 0000 0000 0000 cap1bufh/ velrh capture 1 register, high byte / velocity register, high byte (1) xxxx xxxx uuuu uuuu cap1bufl/ velrl capture 1 register low byte / velocity register, low byte (1) xxxx xxxx uuuu uuuu cap2bufh/ poscnth capture 2 register, high byte / qei position counter register, high byte (1) xxxx xxxx uuuu uuuu cap2bufl/ poscntl capture 2 register, low byte / qei position counter register, low byte (1) xxxx xxxx uuuu uuuu cap3bufh/ maxcnth capture 3 register, high byte / qei max. count limit register, high byte (1) xxxx xxxx uuuu uuuu cap3bufl/ maxcntl capture 3 register, low byte / qei max. count limit register, low byte (1) xxxx xxxx uuuu uuuu cap1con ? cap1ren ? ? cap1m3 cap1m2 cap1m1 cap1m0 -0-- 0000 -0-- 0000 cap2con ? cap2ren ? ? cap2m3 cap2m2 cap2m1 cap2m0 -0-- 0000 -0-- 0000 cap3con ? cap3ren ? ? cap3m3 cap3m2 cap3m1 cap3m0 -0-- 0000 -0-- 0000 dfltcon ? flt4en flt3en flt2en flt1en fltck2 fltck1 fltck0 -000 0000 -000 0000 qeicon velm error up/down qeim2 qeim1 qeim0 pdec1 pdec0 0000 0000 0000 0000 legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition. shaded cells are not used by the motion feedback module. note 1: register name and function determined by which subm odule is selected (ic/qei, respectively). see section 16.1.10 ?other operating modes? for more information.
? 2003 microchip technology inc. preliminary ds39616b-page 181 pic18f2331/2431/4331/4431 17.0 power control pwm module the power control pwm module simplifies the task of generating multiple, synchronized pulse width modulated (pwm) outputs for use in the control of motor controllers and power conversion applications. in particular, the following power and motion control applications are supported by the pwm module:  three-phase and single-phase ac induction motors  switched reluctance motors  brushless dc (bldc) motors  uninterruptible power supplies (ups)  multiple dc brush motors the pwm module has the following features:  up to eight pwm i/o pins with four duty cycle generators. pins can be paired to get a complete half-bridge control.  up to 14-bit resolution, depending upon the pwm period.  ?on-the-fly? pwm frequency changes.  edge- and center-aligned output modes.  single-pulse generation mode.  programmable dead time control between paired pwms.  interrupt support for asymmetrical updates in center-aligned mode.  output override for electrically commutated motor (ecm) operation; for example, bldc.  special event comparator for scheduling other peripheral events.  pwm outputs disable feature sets pwm outputs to their inactive state when in debug mode. the power control pwm module supports three pwm generators and six output channels on pic18f2x31 devices, and four generators and eight channels on pic18f4x31 devices. a simplified block diagram of the module is shown in figure 17-1. figure 17-2 and figure 17-3 show how the module hardware is config- ured for each pwm output pair for the complementary and independent output modes. each functional unit of the pwm module will be discussed in subsequent sections.
pic18f2331/2431/4331/4431 ds39616b-page 182 preliminary ? 2003 microchip technology inc. figure 17-1: power control pwm module block diagram pdc3 pdc3 buffer output driver block pwmcon0 ptper buffer pwmcon1 ptper ptmr comparator comparator channel 3 ptcon sevtcmp comparator special event trigger ovdcon pwm enable and mode pwm manual control pwm generator #2 pwm generator #1 pwm generator #3 (1) sevtdir ptdir dtcon dead time control special event postscaler flta pwm0 pwm1 pwm2 pwm3 pwm4 pwm5 pwm generator #0 fltb (2) note 1: only pwm generator #3 is shown in detail. the other generat ors are identical; their details are omitted for clarity. 2: pwm generator #3 and its logic, pwm channels 6 and 7, and fltb and its associated logic are not implemented on pic18f2x31 devices. pwm6 (2) pwm7 (2) fltcon fault pin control dead time generator and override logic (2) channel 2 dead time generator and override logic channel 1 dead time generator and override logic channel 0 dead time generator and override logic internal data bus 8 8 8 8 8 8 8 8 8 8
? 2003 microchip technology inc. preliminary ds39616b-page 183 pic18f2331/2431/4331/4431 figure 17-2: pwm module bl ock diagram, one output pair, complementary mode figure 17-3: pwm module bl ock diagram, one output pair, independent mode this module contains four duty-cycle generators, numbered 0 through 3. the module has eight pwm output pins, numbered 0 through 7. the eight pwm outputs are grouped into output pairs of even and odd numbered outputs. in complimentary modes, the even pwm pins must always be the complement of the corresponding odd pwm pin. for example, pwm0 will be the complement of pwm1, pwm2 will be the complement of pwm3, and so on. the dead time generator inserts an ?off? period called ?dead time? between the going off of one pin to the going on of the complementary pin of the paired pins. this is to prevent damage to the power switching devices that will be connected to the pwm output pins. the time base for the pwm module is provided by its own 12-bit timer, which also incorporates selectable prescaler and postscaler options. pwm duty cycle register duty cycle comparator dead-band generator fault override values channel override values fault pin assignment logic fault a pin fault b pin hpol lpol pwm1 pwm0 v dd note: in the complementary mode, the even channel cannot be forced active by a fault or override event when the odd channel is active. the even channel is always the complement of the odd channel and is inactive, with dead time inserted, before the odd channel is driven to its active state. duty cycle comparator pwm duty cycle register fault a pin fault b pin hpol lpol pwm1 pwm0 v dd v dd fault override values channel override values fault pin assignment logic
pic18f2331/2431/4331/4431 ds39616b-page 184 preliminary ? 2003 microchip technology inc. 17.1 control registers the operation of the pwm module is controlled by a total of 22 registers. eight of these are used to configure the features of the module:  pwm timer control register 0 (ptcon0)  pwm timer control register 1 (ptcon1)  pwm control register 0 (pwmcon0)  pwm control register 1 (pwmcon1)  dead time control register (dtcon)  output override control register (ovdcond)  output state register (ovdcons)  fault configuration register (fltconfig) there are also 14 registers that are configured as seven register pairs of 16 bits. these are used for the configuration values of specific features. they are:  pwm time base registers (ptmrh and ptmrl)  pwm period registers (ptperh and ptperl)  pwm special event compare registers (sevtcmph and sevtcmpl)  pwm duty cycle #0 registers (pdc0h and pdc0l)  pwm duty cycle #1 registers (pdc1h and pdc1l)  pwm duty cycle #2 registers (pdc2h and pdc2l)  pwm duty cycle #3 registers (pdc3h and pdc3l) all of these register pairs are double-buffered. 17.2 module functionality the pwm module supports several modes of operation that are beneficial for specific power and motor control applications. each mode of operation is described in subsequent sections. the pwm module is composed of several functional blocks. the operation of each is explained separately in relation to the several modes of operation: pwm time base  pwm time base interrupts pwm period  pwm duty cycle  dead time generators  pwm output overrides  pwm fault inputs  pwm special event trigger 17.3 pwm time base the pwm time base is provided by a 12-bit timer with prescaler and postscaler functions. a simplified block diagram of the pwm time base is shown in figure 17-4. the pwm time base is configured through the ptcon0 and ptcon1 registers. the time base is enabled or disabled by respectively setting or clearing the pten bit in the ptcon1 register. note: the ptmr register pair (ptmrl:ptmrh) is not cleared when the pten bit is cleared in software.
? 2003 microchip technology inc. preliminary ds39616b-page 185 pic18f2331/2431/4331/4431 figure 17-4: pwm time base block diagram the pwm time base can be configured for four different modes of operation:  free running mode  single-shot mode  continuous up/down count mode  continuous up/down count mode with interrupts for double updates these four modes are selected by the ptmod1:ptmod0 bits in the ptcon0 register. the free running mode produces edge-aligned pwm generation. the up/down counting modes produce center-aligned pwm generation. the single-shot mode allows the pwm module to support pulse control of certain electronically commutated motors (ecms) and produces edge-aligned operation. ptmr register ptper comparator ptper buffer comparator zero match period match ptmod1 up/down timer reset f osc /4 prescaler 1:1, 1:4, 1:16, 1:64 timer direction control clock control period load duty cycle load ptmod1 period match zero match ptmr clock interrupt control ptmod1 period match zero match ptmod0 postscaler 1:1 - 1:16 ptmod0 pten ptif ptdir ptmr clock update disable (udis)
pic18f2331/2431/4331/4431 ds39616b-page 186 preliminary ? 2003 microchip technology inc. register 17-1: ptcon0: pwm timer cont rol register 0 register 17-2: ptcon1: pwm timer contro l register 1 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ptops3 ptops2 ptops1 ptops0 ptckps1 ptckps0 ptmod1 ptmod0 bit 7 bit 0 bit 7-4 ptops3:ptops0: pwm time base output postscale select bits 0000 =1:1 postscale 0001 =1:2 postscale . . . 1111 =1:16 postscale bit 3-2 ptckps1:ptckps0: pwm time base input clock prescale select bits 00 =pwm time base input clock is fosc/4 (1:1 prescale) 01 =pwm time base input clock is fosc/16 (1:4 prescale) 10 =pwm time base input clock is fosc/64 (1:16 prescale) 11 =pwm time base input clock is fosc/256 (1:64 prescale) bit 1-0 ptmod1:ptmod0: pwm time base mode select bits 11 =pwm time base operates in a continuous up/down mode with interrupts for double pwm updates. 10 =pwm time base operates in a continuous up/down counting mode. 01 =pwm time base configured for single-shot mode. 00 =pwm time base operates in a free running mode. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown r/w-0 r-0 u-0 u-0 u-0 u-0 u-0 u-0 pten ptdir ? ? ? ? ? ? bit 7 bit 0 bit 7 pten: pwm time base timer enable bit 1 = pwm time base is on 0 = pwm time base is off bit 6 ptdir: pwm time base count direction status bit 1 = pwm time base counts down. 0 = pwm time base counts up. bit 5-0 unimplemented: read as ? 0 ?. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1?= bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 187 pic18f2331/2431/4331/4431 register 17-3: pwmcon0: pwm control register 0 u-0 r/w-1 (1) r/w-1 (1) r/w-1 (1) r/w-0 r/w-0 r/w-0 r/w-0 ? pwmen2 pwmen1 pwmen0 pmod3 (3) pmod2 pmod1 pmod0 bit 7 bit 0 bit 7 unimplemented: read as ? 0 ?. bit 6-4 pwmen2:pwmen0: pwm module enable bits (1) 111 =all odd pwm i/o pins enabled for pwm output (2) . 110 =pwm1, pwm3 pins enabled for pwm output. 101 =all pwm i/o pins enabled for pwm output (2) . 100 =pwm0, pwm1, pwm2, pwm3, pwm4 and pwm5 pins enabled for pwm output. 011 =pwm0, pwm1, pwm2 and pwm3 i/o pins enabled for pwm output. 010 =pwm0 and pwm1 pins enabled for pwm output. 001 =pwm1 pin is enabled for pwm output. 000 =pwm module disabled. all pwm i/o pins are general purpose i/o. bit 3-0 pmod3:pmod0: pwm output pair mode bits for pmod0 : 1 = pwm i/o pin pair (pwm0, pwm1) is in the independent mode. 0 = pwm i/o pin pair (pwm0, pwm1) is in the complementary mode. for pmod1 : 1 = pwm i/o pin pair (pwm2, pwm3) is in the independent mode. 0 = pwm i/o pin pair (pwm2, pwm3) is in the complementary mode. for pmod2 : 1 = pwm i/o pin pair (pwm4, pwm5) is in the independent mode. 0 = pwm i/o pin pair (pwm4, pwm5) is in the complementary mode. for pmod3 (3) : 1 = pwm i/o pin pair (pwm6, pwm7) is in the independent mode. 0 = pwm i/o pin pair (pwm6, pwm7) is in the complementary mode. note 1: reset condition of pwmen bits depends on pwmpin device configuration bit. 2: when pwmen2:pwmen0 = 101 , pwm[5:0] outputs are enabled for pic18f2x31 devices; pwm[7:0] outputs are enabled for pic18f4x31devices. when pwmen2:pwmen0 = 111 , pwm outputs 1, 3 and 5 are enabled in pic18f2x31devices; pwm outputs 1, 3, 5 and 7 are enabled in pic18f4x31 devices. 3: unimplemented in pic18f2x31 devices; maintain these bits clear. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 188 preliminary ? 2003 microchip technology inc. register 17-4: pwmcon1: pwm control register 1 17.3.1 free running mode in the free running mode, the pwm time base (ptmrl and ptmrh) will begin counting upwards until the value in the time base period register, ptper (ptperl and ptperh), is matched. the ptmr regis- ters will be reset on the following input clock edge and the time base will continue counting upwards as long as the pten bit remains set. 17.3.2 single-shot mode in the single-shot mode, the pwm time base will begin counting upwards when the pten bit is set. when the value in the ptmr register matches the ptper regis- ter, the ptmr register will be reset on the following input clock edge and the pten bit will be cleared by the hardware to halt the time base. 17.3.3 continuous up/down counting modes in continuous up/down counting modes, the pwm time base counts upwards until the value in the ptper register matches with ptmr. on the following input clock edge, the timer counts downwards. the ptdir bit in the ptcon1 register is read-only and indicates the counting direction. the ptdir bit is set when the timer counts downwards. 17.3.4 pwm time base prescaler the input clock to ptmr (f osc /4) has prescaler options of 1:1, 1:4, 1:16 or 1:64. these are selected by control bits ptckps<1:0> in the ptcon0 register. the prescaler counter is cleared when any of the following occurs:  write to the ptmr register  write to the ptcon (ptcon0 or ptcon1) register  any device reset table 17-1 shows the minimum pwm frequencies that can be generated with the pwm time base and the prescaler. an operating frequency of 40 mhz (f cyc = 10 mhz) and ptper = 0xfff is assumed in the table. the pwm module must be capable of gener- ating pwm signals at the line frequency (50 hz or 60 hz) for certain power control applications. r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 sevops3 sevops2 sevops1 sevops0 sevtdir ? udis osync bit 7 bit 0 bit 7-4 sevops3:sevops0: pwm special event trigger output postscale select bits 0000 =1:1 postscale 0001 =1:2 postscale . . . 1111 =1:16 postscale bit 3 sevtdir: special event trigger time base direction bit 1 = a special event trigger will occur when the pwm time base is counting downwards. 0 = a special event trigger will occur when the pwm time base is counting upwards. bit 2 unimplemented: read as ? 0 ?. bit 1 udis: pwm update disable bit 1 = updates from duty cycle and period buffer registers are disabled. 0 = updates from duty cycle and period buffer registers are enabled. bit 0 osync: pwm output override synchronization bit 1 = output overrides via the ovdcon register are synchronized to the pwm time base. 0 = output overrides via the ovdcon register are asynchronous. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown note: when the pwm timer is enabled in up/down count mode, during the first half of the first period of the up/down counting modes, the pwm outputs are kept inactive. by doing this, pwm pins will output garbage duty cycle due to unknown value in the ptmr registers. note: the ptmr register is not cleared when ptcon is written.
? 2003 microchip technology inc. preliminary ds39616b-page 189 pic18f2331/2431/4331/4431 table 17-1: minimum pwm frequency 17.3.5 pwm time base postscaler the match output of ptmr can optionally be post-scaled through a 4-bit postscaler (which gives a 1:1 to 1:16 scaling inclusive) to generate an interrupt. the postscaler counter is cleared when any of the following occurs:  write to the ptmr register  write to the ptcon register  any device reset the ptmr register is not cleared when ptcon is written. 17.4 pwm time base interrupts the pwm timer can generate interrupts based on the modes of operation selected by ptmod<1:0> bits and the postscaler bits (ptops<3:0>). 17.4.1 interrupts in free running mode when the pwm time base is in the free running mode (ptmod<1:0> = 00 ), an interrupt event is generated each time a match with the ptper register occurs. the ptmr register is reset to zero in the following clock edge. using a postscaler selection other than 1:1 will reduce the frequency of interrupt events. figure 17-5: pwm time base interrupt timing, free running mode minimum pwm frequencies vs. prescaler value for f cyc = 10 mips, (ptper = 0fffh) prescale pwm frequency edge-aligned pwm frequency center-aligned 1:1 2441 hz 1221 hz 1:4 610 hz 305 hz 1:16 153 hz 76 hz 1:64 38 hz 19 hz q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 f osc /4 ptmr_int_req ffeh fffh 000h 001h 002h ptif bit 1 note 1: pwm time base period register, ptper, is loaded with the value fffh for this example. qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc ptif bit ptmr ffeh fffh 000h 001h 002h 1 a: prescaler = 1:1 b: prescaler = 1:4 ptmr ptmr_int_req q4 q4
pic18f2331/2431/4331/4431 ds39616b-page 190 preliminary ? 2003 microchip technology inc. 17.4.2 interrupts in single-shot mode when the pwm time base is in the single-shot mode (ptmod<1:0> = 01 ), an interrupt event is generated when a match with the ptper register occurs. the pwm timer register (ptmr) is reset to zero on the following input clock edge, and the pten bit is cleared. the postscaler selection bits have no effect in this timer mode. 17.4.3 interrupts in continuous up/down counting mode in the up/down counting mode (ptmod<1:0> = 10 ), an interrupt event is generated each time the value of the ptmr register becomes zero and the pwm time base begins to count upwards. the postscaler selection bits may be used in this mode of the timer to reduce the frequency of the interrupt events. figure 17-7 shows the interrupts in continuous up/down counting mode. figure 17-6: pwm time base interrupt timing, single shot mode q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 1 1 f osc /4 ptmr ffeh fffh 000h 000h 000h 1 ptif bit 2 note 1: interrupt flag bit ptif is sampled here (every q1). 2: pwm time base period register, ptper, is loaded with the value fffh for this example. qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc 1 1 ptmr ffeh fffh 000h 000h 000h 1 2 a: prescaler = 1:1 b: prescaler = 1:4 ptmr_int_req q4 q4 ptif bit ptmr_int_req
? 2003 microchip technology inc. preliminary ds39616b-page 191 pic18f2331/2431/4331/4431 figure 17-7: pwm time base interrupts, up/down counting mode q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 f osc /4 ptmr 002h 001h 000h 001h 002h note 1: interrupt flag bit ptif is sampled here (every q1). qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc qc 1 1 ptmr 002h 001h 000h 001h 002h 1 ptdir bit 1 prescaler = 1:1 prescaler = 1:4 ptif bit ptmr_int_req ptdir bit q4 q4 ptif bit ptmr_int_req 1 1 1 1
pic18f2331/2431/4331/4431 ds39616b-page 192 preliminary ? 2003 microchip technology inc. 17.4.4 interrupts in double update mode this mode is available in up/down counting mode. in the double update mode (ptmod<1:0> = 11 ), an interrupt event is generated each time the ptmr register is equal to zero and each time the ptmr matches with ptper register. figure 17-8 shows the interrupts in up/down counting mode with double updates. the double update mode provides two additional functions to the user in center-aligned mode. 1. the control loop bandwidth is doubled because the pwm duty cycles can be updated twice per period. 2. asymmetrical center-aligned pwm waveforms can be generated, which are useful for minimizing output waveform distortion in certain motor control applications. figure 17-8: pwm time base interrupts, up/down counting mode with double updates note: do not change ptmod while pten is active. it will yield unexpected results. to change pwm timer mode of operation, first clear pten bit, load ptmod with required data and then set pten. q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 1 1 osc1 ptmr 3fdh 3feh 3ffh 3feh 3fdh 1 case 1: ptmr counting upwards q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 q2 q1 q3 q4 1 1 osc1 ptmr 002h 001h 000h 001h 002h 1 case 2: ptmr counting downwards 2 note 1: interrupt flag bit ptif is sampled here (every q1). 2: pwm time base period register, ptper, is loaded with the value 3ffh for this example. 1 1 ptif bit ptmr_int_req ptif bit ptmr_int_req a: prescaler = 1:1 ptdir bit ptdir bit
? 2003 microchip technology inc. preliminary ds39616b-page 193 pic18f2331/2431/4331/4431 17.5 pwm period the pwm period is defined by the ptper register pair (ptperl and ptperh). the pwm period has 12-bit resolution by combining 4 lsbs of ptperh and 8-bits of ptperl. ptper is a double-buffered register used to set the counting period for the pwm time base. the ptper buffer contents are loaded into the ptper register at the following times:  free running and single-shot modes: when the ptmr register is reset to zero after a match with the ptper register.  up/down counting modes: when the ptmr register is zero. the value held in the ptper buffer is automatically loaded into the ptper register when the pwm time base is disabled (pten = 0 ). figure 17-9 and figure 17-10 indicate the times when the contents of the ptper buffer are loaded into the actual ptper register. the pwm period can be calculated from the following formulas: equation 17-1: pwm period for free running mode equation 17-2: pwm period for up/down counting mode the pwm frequency is the inverse of period; or the maximum resolution (in bits) for a given device oscillator and pwm frequency can be determined from the following formula: equation 17-3: pwm resolution the pwm resolutions and frequencies are shown for a selection of execution speeds and ptper values in table 17-2. the pwm frequencies in table 17-2 are calculated for edge-aligned pwm mode. for center-aligned mode, the pwm frequencies will be approximately one-half the values indicated in this table. table 17-2: example pwm frequencies and resolutions tpwm = (ptper + 1) fosc/(ptmrps/4) tpwm = (ptper + 1) x ptmrps fosc/4 or tpwm = (2 x ptper) fosc/(ptmrps/4) pwm frequency 1 pwm period ------------------------------ - = pwm frequency = 1/tpwm fosc mips ptper value pwm resolution pwm frequency 40 mhz 10 0fffh 14 bits 2.4 khz 40 mhz 10 07ffh 13 bits 4.9 khz 40 mhz 10 03ffh 12 bits 9.8 khz 40 mhz 10 01ffh 11 bits 19.5 khz 40 mhz 10 ffh 10 bits 39.0 khz 40 mhz 10 7fh 9 bits 78.1 khz 40 mhz 10 3fh 8 bits 156.2 khz 40 mhz 10 1fh 7 bits 312.5 khz 40 mhz 10 0fh 6 bits 625 khz 25 mhz 6.25 0fffh 14 bits 1.5 khz 25 mhz 6.25 03ffh 12 bits 6.1 khz 25 mhz 6.25 ffh 10 bits 24.4 khz 10 mhz 2.5 0fffh 14 bits 610 hz 10 mhz 2.5 03ffh 12 bits 2.4 khz 10 mhz 2.5 ffh 10 bits 9.8 khz 5 mhz 1.25 0fffh 14 bits 305 hz 5 mhz 1.25 03ffh 12 bits 1.2 khz 5 mhz 1.25 ffh 10 bits 4.9 khz 4 mhz 1 0fffh 14 bits 244 hz 4 mhz 1 03ffh 12 bits 976 hz 4 mhz 1 ffh 10 bits 3.9 khz note: for center-aligned operation, pwm frequencies will be approximately 1/2 the value indicated in the table. resolution = log(2) log fosc/4 fpwm
pic18f2331/2431/4331/4431 ds39616b-page 194 preliminary ? 2003 microchip technology inc. figure 17-9: pwm period buffer updates in free running count mode figure 17-10: pwm period buffer updates in up/down counting modes old ptper value = 004 new ptper value = 007 period value loaded from ptper buffer register new value written to ptper buffer. 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 5 6 7 old ptper value = 004 new ptper value = 007 period value loaded from ptper buffer register new value written to ptper buffer. 0 1 2 3 4 3 2 1 0 1 2 3 4 5 6 7 3 2 1 0 4 5 6
? 2003 microchip technology inc. preliminary ds39616b-page 195 pic18f2331/2431/4331/4431 17.6 pwm duty cycle pwm duty cycle is defined by pdcx (pdcxl and pdcxh) registers. there are a total of 4 pwm duty cycle registers for 4 pairs of pwm channels. the duty cycle registers have 14-bit resolution by combining 6 lsbs of pdcxh with the 8 bits of pdcxl. pdcx is a double-buffered register used to set the counting period for the pwm time base. 17.6.1 pwm duty cycle registers there are four 14-bit special function registers used to specify duty cycle values for the pwm module:  pdc0 (pdc0l and pdc0h)  pdc1 (pdc1l and pdc1h)  pdc2 (pdc2l and pdc2h)  pdc3 (pdc3l and pdc3h) the value in each duty cycle register determines the amount of time that the pwm output is in the active state. the upper 12 bits of pdcn hold the actual duty cycle value from ptmrh/l<11:0>, while the lower 2 bits control which internal q-clock the duty cycle match occurs. this 2-bit value is decoded from the q-clocks as shown in figure 17-11 (when the prescaler is 1:1 (ptckps = 00 )). in edge-aligned mode, the pwm period starts at q1 and ends when the duty cycle register matches the ptmr register as follows. the duty cycle match is con- sidered when the upper 12 bits of the pdc is equal to the ptmr and the lower 2 bits are equal to q1, q2, q3 or q4, depending on the lower two bits of the pdc (when the prescaler is 1:1, or ptckps = 00 ) . each compare unit has logic that allows override of the pwm signals. this logic also ensures that the pwm signals will complement each other (with dead time insertion) in complementary mode (see section 17.7 ?dead time generators? ). figure 17-11: duty cycle comparison note: when prescaler is not 1:1 (ptckps ~ 00 ), the duty cycle match occurs at q1 clock of the instruction cycle when the ptmr and pdc match occurs. ptmr<11:0> pdcn<13:0> comparator unused unused ptmrh<7:0> ptmrl<7:0> ptmrh<3:0> ptmrl<7:0> q-clocks (1) pdcnh<7:0> pdcnl<7:0> pdcnh<5:0> pdcnl<7:0> <1:0> note 1: this value is decoded from the q-clocks: 00 = duty cycle match occurs on q1 01 = duty cycle match occurs on q2 10 = duty cycle match occurs on q3 11 = duty cycle match occurs on q4
pic18f2331/2431/4331/4431 ds39616b-page 196 preliminary ? 2003 microchip technology inc. 17.6.2 duty cycle register buffers the four pwm duty cycle registers are double-buffered to allow glitchless updates of the pwm outputs. for each duty cycle block, there is a duty cycle buffer register that is accessible by the user and a second duty cycle register that holds the actual compare value used in the present pwm period. in edge-aligned pwm output mode, a new duty cycle value will be updated whenever a ptmr match with the ptper register occurs and ptmr is reset as shown in figure 17-12. also, the contents of the duty cycle buffers are automatically loaded into the duty cycle registers when the pwm time base is disabled (pten = 0 ). when the pwm time base is in the up/down counting mode, new duty cycle values will be updated when the value of the ptmr register is zero and the pwm time base begins to count upwards. the contents of the duty cycle buffers are automatically loaded into the duty cycle registers when the pwm time base is disabled (pten = 0 ). figure 17-13 shows the timings when the duty cycle update occur for the up/down count mode. in this mode, up to one entire pwm period is available for calculating and loading the new pwm duty cycle before changes take effect. when the pwm time base is in the up/down counting mode with double updates, new duty cycle values will be updated when the value of the ptmr register is zero and when the value of the ptmr register matches the value in the ptper register. the contents of the duty cycle buffers are automatically loaded into the duty cycle registers during both of the above said conditions. figure 17-14 shows the duty cycle updates for up/down mode with double update. in this mode, only up to half of a pwm period is available for calculating and loading the new pwm duty cycle before changes take effect. 17.6.3 edge-aligned pwm edge-aligned pwm signals are produced by the module when the pwm time base is in the free running mode or the single-shot mode. for edge-aligned pwm outputs, the output for a given pwm channel has a period specified by the value loaded in ptper and a duty cycle specified by the appropriate duty cycle register (see figure 17-12). the pwm output is driven active at the beginning of the period (ptmr = 0 ) and is driven inactive when the value in the duty cycle register matches ptmr. a new cycle is started when ptmr matches the ptper as explained in the pwm period section. if the value in a particular duty cycle register is zero, then the output on the corresponding pwm pin will be inactive for the entire pwm period. in addition, the out- put on the pwm pin will be active for the entire pwm period if the value in the duty cycle register is greater than the value held in the ptper register. figure 17-12: edge-aligned pwm figure 17-13: duty cycle update times in up/down counting mode period duty cycle 0 ptper ptmr value new duty cycle latche d active at of period beginning pdc pdc (new) (old) ptmr value pwm output duty cycle value loaded from buffer register new value written to duty cycle buffer
? 2003 microchip technology inc. preliminary ds39616b-page 197 pic18f2331/2431/4331/4431 figure 17-14: duty cycle update times in up/down counting mode with double updates 17.6.4 center-aligned pwm center-aligned pwm signals are produced by the module when the pwm time base is configured in an up/down counting mode (see figure 17-15). the pwm compare output is driven to the active state when the value of the duty cycle register matches the value of ptmr and the pwm time base is counting downwards (ptdir = 1 ). the pwm compare output will be driven to the inactive state when the pwm time base is counting upwards (ptdir = 0 ) and the value in the ptmr register matches the duty cycle value. if the value in a particular duty cycle register is zero, then the output on the corresponding pwm pin will be inactive for the entire pwm period. in addition, the output on the pwm pin will be active for the entire pwm period if the value in the duty cycle register is equal to or greater than the value in the ptper register. figure 17-15: start of center-aligned pwm ptmr value pwm output duty cycle value loaded from buffer register new values written to duty cycle buffer. note: when the pwm is started in center-aligned mode, the period register (ptper) is loaded into the pwm timer register (ptmr) and the ptmr is configured automatically to start down-counting. this is done to ensure that all the pwm signals don?t start at the same time. 0 ptper ptmr value period period/2 duty cycle start of first pwm period period duty cycle
pic18f2331/2431/4331/4431 ds39616b-page 198 preliminary ? 2003 microchip technology inc. 17.6.5 complementary pwm operation the complementary mode of pwm operation is useful to drive one or more power switches in half-bridge configuration as shown in figure 17-16. this inverter topology is typical for a 3-phase induction motor, brushless dc motor or a 3-phase uninterruptible power supply (ups) control applications. each upper/lower power switch pair is fed by a complementary pwm signal. dead time may be optionally inserted during device switching where both outputs are inactive for a short period (see section 17.7 ?dead time generators? ). in complementary mode, the duty cycle comparison units are assigned to the pwm outputs as follows:  pdc0 register controls pwm1/pwm0 outputs  pdc1 register controls pwm3/pwm2 outputs  pdc2 register controls pwm5/pwm4 outputs  pdc3 register controls pwm7/pwm6 outputs pwm1/3/5/7 are the main pwms that are controlled by the pdc registers and pwm0/2/4/6 are the complemented outputs. when using the pwms to control the half bridge, the odd number pwms can be used to control the upper power switch and the even numbered pwms for the lower switches. figure 17-16: typical load for complementary pwm outputs the complementary mode is selected for each pwm i/o pin pair by clearing the appropriate pmodx bit in the pwmcon0 register. the pwm i/o pins are set to complementary mode by default upon all kinds of device resets. +v pwm1 pwm0 pwm3 pwm2 pwm5 pwm4 3 phase load
? 2003 microchip technology inc. preliminary ds39616b-page 199 pic18f2331/2431/4331/4431 17.7 dead time generators in power inverter applications where the pwms are used in complementary mode to control the upper and lower switches of a half-bridge, a dead time insertion is highly recommended. the dead time insertion keeps both outputs in inactive state for a brief time. this avoids any overlap in the switching during the state change of the power devices due to t on and t off characteristics. because the power output devices cannot switch instantaneously, some amount of time must be pro- vided between the turn-off event of one pwm output in a complementary pair and the turn-on event of the other transistor. the pwm module allows dead time to be programmed. following sections explain the dead time block in detail. 17.7.1 dead time insertion each complementary output pair for the pwm module has a 6-bit down counter used to produce the dead time insertion. as shown in figure 17-17, each dead time unit has a rising and falling edge detector con- nected to the duty cycle comparison output. the dead time is loaded into the timer on the detected pwm edge event. depending on whether the edge is rising or fall- ing, one of the transitions on the complementary out- puts is delayed until the timer counts down to zero. a timing diagram indicating the dead time insertion for one pair of pwm outputs is shown in figure 17-18. figure 17-17: dead time control unit bl ock diagram for one pwm output pair figure 17-18: dead time inse rtion for complementary pwm even pwm signal to output control block odd pwm signal to output control block zero compare clock control and prescaler 6-bit down counter duty cycle compare input dead time prescale f osc dead time select bits dead time register t d pdc1 compare output pwm1 pwm0 t d
pic18f2331/2431/4331/4431 ds39616b-page 200 preliminary ? 2003 microchip technology inc. register 17-5: dtcon ? dead time cont rol register 17.7.2 dead time ranges the amount of dead time provided by the dead time unit is selected by specifying the input clock prescaler value and a 6-bit unsigned value defined in the dtcon register. four input clock prescaler selections have been provided to allow a suitable range of dead times based on the device operating frequency. f osc /2, f osc /4, f osc /8 and f osc /16 are the clock prescaler options available using the dtps1:dtps0 control bits in the dtcon register. after selecting an appropriate prescaler value, the dead time is adjusted by loading a 6-bit unsigned value into dtcon<5:0>. the dead time unit prescaler is cleared on any of the following events:  on a load of the down timer due to a duty cycle comparison edge event;  on a write to the dtcon register; or  on any device reset. 17.7.3 decrementing the dead time counter the dead time counter is clocked from any of the q clocks based on the following conditions. 1. the dead time counter is clocked on q1 when:  the dtps bits are set to any of the following dead time prescaler settings: fosc/4, f osc /8, f osc /16  the pwm time base prescale bits (ptckps) are set to any of the following prescale ratios: f osc /16, f osc /64, f osc /256. 2. the dead time counter is clocked by a pair of q-clocks when the pwm time base prescale bits are set to 1:1 (ptckps1:ptckps0 = 00 , f osc /4) and the dead time counter is clocked by the f osc /2 (dtps1:dtps0 = 00 ). 3. the dead time counter is clocked using every other q-clock depending on the two lsbs in the duty cycle registers:  if the pwm duty cycle match occurs on q1 or q3, then the dead time counter is clocked using every q1 and q3.  if the pwm duty cycles match occurs on q2 or q4, then the dead time counter is clocked using every q2 and q4. 4. when the dtps1:dtps0 bits are set to any of the other dead time prescaler settings, (i.e., f osc /4, f osc /8 or f osc /16) and the pwm time base prescaler is set to 1:1, the dead time counter is clocked by the q-clock corresponding to the q-clocks on which the pwm duty cycle match occurs. the actual dead time is calculated from the dtcon register as follows: dead time = dead time value / (f osc /prescaler) table 17-3 shows example dead time ranges as a function of the input clock prescaler selected and the device operating frequency. r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 dtps1 dtps0 dt5 dt4 dt3 dt2 dt1 dt0 bit 7 bit 0 bit 7-6 dtps1:dtps0: dead time unit a prescale select bits 11 = clock source for dead time unit is f osc /16. 10 = clock source for dead time unit is f osc /8. 01 = clock source for dead time unit is f osc /4. 00 = clock source for dead time unit is f osc /2. bit 5-0 dt5:dt0: unsigned 6-bit dead time value bits for dead time unit. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 201 pic18f2331/2431/4331/4431 table 17-3: example dead time ranges 17.7.4 dead time distortion 17.8 independent pwm output independent pwm mode is used for driving the loads as shown in figure 17-19 for driving one winding of a switched reluctance motor. a particular pwm output pair is configured in the independent output mode when the corresponding pmod bit in the pwmcon0 register is set. no dead time control is implemented between the pwm i/o pins when the module is operat- ing in the independent mode and both i/o pins are allowed to be active simultaneously. this mode can also be used to drive stepper motors. 17.8.1 duty cycle assignment in the independent mode in the independent mode, each duty cycle generator is connected to both pwm output pins in a given pwm output pair. the odd and the even pwm output pins are driven with a single pwm duty cycle generator. pwm1 and pwm0 are driven by the pwm channel which uses pdc0 register to set the duty cycle, pwm3 and pwm2 with pdc1, pwm5 and pwm4 with pdc2, pwm7 and pwm6 with pdc3, see figure 17-3 and register 17-3. fosc (mhz) mips prescaler selection dead time min dead time max 40 10 f osc /2 50 ns 3.2 s 40 10 f osc /4 100 ns 6.4 s 40 10 f osc /8 200 ns 12.8 s 40 10 f osc /16 400 ns 25.6 s 32 8 f osc /2 62.5 ns 4 s 32 8 f osc /4 125 ns 8 s 32 8 f osc /8 250 ns 16 s 32 8 f osc /16 500 ns 32 s 25 6.25 f osc /2 80 ns 5.12 vs 25 6.25 f osc /4 160 ns 10.2 s 25 6.25 f osc /8 320 ns 20.5 s 25 6.25 f osc /16 640 ns 41 s 20 5 f osc /2 100 ns 6.4 s 20 5 f osc /4 200 ns 12.8 s 20 5 f osc /8 400 25.6 vs 20 5 f osc /16 800 51.2 s 10 2.5 f osc /2 200 ns 12.8 s 10 2.5 f osc /4 400 ns 25.6 s 10 2.5 f osc /8 800 ns 51.2 s 10 2.5 f osc /16 1.6 s 102.4 s 51.25f osc /2 400 ns 25.6 s 51.25f osc /4 800 ns 51.2 s 51.25f osc /8 1.6 s 102.4 s 51.25f osc /16 3.2 s 204.8 s 41f osc /2 0.5 s 32 s 41f osc /4 1 s 64 s 41f osc /8 2 s 128 s 41f osc /16 4 s 256 s note 1: for small pwm duty cycles, the ratio of dead time to the active pwm time may become large. in this case, the inserted dead time will introduce distortion into waveforms produced by the pwm mod- ule. the user can ensure that dead time distortion is minimized by keeping the pwm duty cycle at least three times larger than the dead time. a similar effect occurs for duty cycles at or near 100%. the maximum duty cycle used in the application should be chosen such that the minimum inactive time of the signal is at least three times larger than the dead time. if the dead time is greater or equal to the duty cycle of one of the pwm out- puts pairs, then that pwm pair will be inactive for the whole period. 2: changing the dead time values in dtcon when the pwm is enabled may result in undesired situation. disable the pwm (pten = 0 ) before changing the dead time value
pic18f2331/2431/4331/4431 ds39616b-page 202 preliminary ? 2003 microchip technology inc. 17.8.2 pwm channel override pwm output may be manually overridden for each pwm channel by using the appropriate bits in the ovdcond and ovdcons registers. the user may select the following signal output options for each pwm output pin operating in the independent mode:  i/o pin outputs pwm signal  i/o pin inactive  i/o pin active refer to section 17.10 ?pwm output override? for details for all the override functions. figure 17-19: center-connected load 17.9 single-pulse pwm operation the single pulse pwm operation is available only in edge-aligned mode. in this mode, the pwm module will produce single pulse output. single-pulse operation is configured when the ptmod1:ptmod0 bits are set to ? 01 ? in ptcon0 register. this mode of operation is use- ful for driving certain types of ecms. in single-pulse mode, the pwm i/o pin(s) are driven to the active state when the pten bit is set. when the pwm timer match with duty cycle register occurs, the pwm i/o pin is driven to the inactive state. when the pwm timer match with the ptper register occurs, the ptmr register is cleared, all active pwm i/o pins are driven to the inactive state, the pten bit is cleared, and an interrupt is generated, if the corresponding interrupt bit is set. 17.10 pwm output override the pwm output override bits allow the user to manu- ally drive the pwm i/o pins to specified logic states independent of the duty cycle comparison units. the pwm override bits are useful when controlling various types of ecms like a bldc motor. ovdcond and ovdcons registers are used to define the pwm override options. the ovdcond register contains eight bits, povd7:povd0, that determine which pwm i/o pins will be overridden. the ovdcons register contains eight bits, pout7:pout0, that determine the state of the pwm i/o pins when a particular output is overridden via the povd bits. the povd bits are active-low control bits. when the povd bits are set, the corresponding pout bit will have no effect on the pwm output. in other words, the pins corresponding to povd bits that are set will have the duty pwm cycle set by the pdc registers. when one of the povd bits is cleared, the output on the cor- responding pwm i/o pin will be determined by the state of the pout bit. when a pout bit is set, the pwm pin will be driven to its active state. when the pout bit is cleared, the pwm pin will be driven to its inactive state. 17.10.1 complementary output mode the even-numbered pwm i/o pin has override restric- tions when a pair of pwm i/o pins are operating in the complementary mode (pmodx = 0 ). in complemen- tary mode, if the even-numbered pin is driven active by clearing the corresponding povd bit and by setting pout bits in ovdcond and ovdcons registers, the output signal is forced to be the complement of the odd-numbered i/o pin in the pair (see figure 17-2 for details). 17.10.2 override synchronization if the osync bit in the pwmcon1 register is set, all output overrides performed via the ovdcond and ovdcons registers will be synchronized to the pwm time base. synchronous output overrides will occur on following conditions:  when the pwm is in edge-aligned mode, syn- chronization occurs when ptmr is zero.  when the pwm is in center-aligned mode, synchronization occurs when ptmr is zero and when the value of ptmr matches ptper. note: ptper and pdc values are held as it is after the single pulse output. to have another cycle of single pulse, only pten has to be enabled. +v pwm1 pwm0 load note 1: in the complementary mode, the even channel cannot be forced active by a fault or override event when the odd channel is active. the even channel is always the complement of the odd channel with dead time inserted, before the odd channel can be driven to its active state as shown in figure 17-20. 2: dead time inserted to the pwm channels even when they are in override mode.
? 2003 microchip technology inc. preliminary ds39616b-page 203 pic18f2331/2431/4331/4431 figure 17-20: override bits in complementary mode 17.10.3 output override examples figure 17-21 shows an example of a waveform that might be generated using the pwm output override feature. the figure shows a six-step commutation sequence for a bldc motor. the motor is driven through a 3-phase inverter as shown in figure 17-16. when the appropriate rotor position is detected, the pwm outputs are switched to the next commutation state in the sequence. in this example, the pwm out- puts are driven to specific logic states. the ovdcond and ovdcons register values used to generate the signals in figure 17-21 are given in table 17-4. the pwm duty cycle registers may be used in con- junction with the ovdcond and ovdcons registers. the duty cycle registers control the average voltage across the load and the ovdcond and ovdcons registers control the commutation sequence. figure 17-22 shows the waveforms, while table 17-4 and table 17-5 show the ovdcond and ovdcons register values used to generate the signals. register 17-6: ovdcond: output override control register 1. even override bits have no effect in complementary mode. 2. odd override bit is activated, which causes the even pwm to deactivate. 3. dead time insertion. 4. odd pwm activated after the dead time. 5. odd override bit is deactivated, which causes the odd pwm to deactivate. 6. dead time insertion. 7. even pwm is activated after the dead time. pout0 pout1 pwm1 pwm0 assume: pvod0 = 0 ; pvod1 = 0 ; pmod0 = 0 6 5 4 3 2 7 1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 povd7 (1) povd6 (1) povd5 povd4 povd3 povd2 povd1 povd0 bit 7 bit 0 bit 7-0 povd7:povd0: pwm output override bits (1) 1 = output on pwm i/o pin is controlled by the value in the duty cycle register and the pwm time base. 0 = output on pwm i/o pin is controlled by the value in the corresponding pout bit. note 1: unimplemented in pic18f2x31 devices; maintain these bits clear. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 204 preliminary ? 2003 microchip technology inc. register 17-7: ovdcons: output state register figure 17-21: pwm output override example #1 table 17-4: pwm output override example #1 table 17-5: pwm output override example #2 figure 17-22: pwm output override example #2 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 pout7 (1) pout6 (1) pout5 pout4 pout3 pout2 pout1 pout0 bit 7 bit 0 bit 7-0 pout7:pout0: pwm manual output bits (1) 1 = output on pwm i/o pin is active when the corresponding pwm output override bit is cleared. 0 = output on pwm i/o pin is inactive when the corresponding pwm output override bit is cleared. note 1: unimplemented in pic18f2x31 devices; maintain these bits clear. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown state ovdcond(povd) ovdcons(pout) 1 00000000b 00100100b 2 00000000b 00100001b 3 00000000b 00001001b 4 00000000b 00011000b 5 00000000b 00010010b 6 00000000b 00000110b state ovdcond (povd) ovdcons (pout) 1 11000011b 00000000b 2 11110000b 00000000b 3 00111100b 00000000b 4 00001111b 00000000b pwm5 pwm4 pwm3 pwm2 pwm1 pwm0 12345 6 pwm7 pwm6 pwm5 pwm4 pwm3 pwm2 123 4 pwm1 pwm0
? 2003 microchip technology inc. preliminary ds39616b-page 205 pic18f2331/2431/4331/4431 17.11 pwm output and polarity control there are three device configuration bits associated with the pwm module that provide pwm output pin control defined in config3l configuration register.  hpol configuration bit  lpol configuration bit  pwmpin configuration bit these three configuration bits work in conjunction with the three pwm enable bits (pwmen2:pwmen0) in the pwmcon0 register. the configuration bits and pwm enable bits ensure that the pwm pins are in the correct states after a device reset occurs. 17.11.1 output pin control the pwmen2:pwmen0 control bits enable each pwm output pin as required in the application. all pwm i/o pins are general purpose i/o. when a pair of pins are enabled for pwm output, the port and tris registers controlling the pin are disabled. refer to figure 17-23 for details. figure 17-23: pwm i/o pin block diagram 17.11.2 output polarity control the polarity of the pwm i/o pins is set during device programming via the hpol and lpol configuration bits in the config3l device configuration register. the hpol configuration bit sets the output polarity for the high-side pwm outputs, pwm1, pwm3, pwm5 and pwm7. the polarity is active-high when hpol is cleared (= 0 ), and active-low when it is set (= 1 ). the lpol configuration bit sets the output polarity for the low-side pwm outputs, pwm0, pwm2, pwm4 and pwm6. as with hpol, they are active-high when lpol is cleared, and active-low when set. all output signals generated by the pwm module are referenced to the polarity control bits, including those generated by fault inputs or manual override (see section 17.10 ?pwm output override? ). the default polarity configuration bits have the pwm i/o pins in active-high output polarity. data bus wr port wr tris rd port data latch tris latch p v ss i/o pin q d q ck q d q ck qd en n v dd rd tris schmitt trigger ttl or 0 1 pwm pin enable pwm signal from module note: i/o pin has protection diodes to v dd and v ss . pwm polarity selection logic not shown for clarity.
pic18f2331/2431/4331/4431 ds39616b-page 206 preliminary ? 2003 microchip technology inc. 17.11.3 pwm output pin reset states the pwmpin configuration bit determines the pwm output pins to be pwm output pins or digital i/o pins, after the device comes out of reset. if the pwmpin con- figuration bit is unprogrammed (default), the pwmen2:pwmen0 control bits will be cleared on a device reset. consequently, all pwm outputs will be tri-stated and controlled by the corresponding port and tris registers. if the pwmpin configuration bit is programmed low, the pwmen2:pwmen0 control bits will be set as follows on a device reset:  pwmen2:pwmen0 = 101 if device has 8 pwm pins (pic18f4x31 devices)  pwmen2:pwmen0 = 100 if device has 6 pwm pins (pic18f2x31 devices) all pwm pins will be enabled for pwm output and will have the output polarity defined by the hpol and lpol configuration bits. 17.12 pwm fault inputs there are two fault inputs associated with the pwm module. the main purpose of the input fault pins is to disable the pwm output signals and drive them into an inactive state. the action of the fault inputs is performed directly in hardware so that when a fault occurs, it can be managed quickly and the pwms outputs are put into an inactive state to save the power devices connected to the pwms. the pwm fault inputs are flta and fltb , which can come from i/o pins, the cpu or another module. the flta and fltb pins are active-low inputs so it is easy to ?or? many sources to the same input. the fltconfig register (register 17-8) defines the settings of flta and fltb inputs. 17.12.1 fault pin enable bits by setting the bits fltaen and fltben in the fltconfig register, the corresponding fault inputs are enabled. if both bits are cleared, then the fault inputs have no effect on the pwm module. 17.12.2 mfault input modes the fltamod and fltbmod bits in the fltconfig register determine the modes of pwm i/o pins that are deactivated when they are overridden by fault input. fltas and fltbs bits in the fltconfig register give the status of faulta and faultb inputs. each of the fault inputs have two modes of operation:  inactive mode (fltxmod = 0 ) this is a catastrophic fault management mode. when the fault occurs in this mode, the pwm out- puts are deactivated. the pwm pins will remain in inactivated mode until the fault is cleared (fault input is driven high) and the corresponding fault status bit has been cleared in software. the pwm outputs are enabled immediately at the beginning of the following pwm period, after fault status bit (fltxs) is cleared.  cycle-by-cycle mode (fltxmod = 1 ) when the fault occurs in this mode, the pwm out- puts are deactivated. the pwm outputs will remain in the defined fault states (all pwm outputs inactive) for as long as the fault pin is held low. after the fault pin is driven high, the pwm outputs will return to normal operation at the beginning of the following pwm period, and the flts bit is automatically cleared. note: the inactive state of the pwm pins are dependent on the hpol and lpol config- uration bit settings, which defines the active and inactive state for pwm outputs.
? 2003 microchip technology inc. preliminary ds39616b-page 207 pic18f2331/2431/4331/4431 17.12.3 pwm outputs while in fault condition while in the fault state (i.e., one or both flta and fltb inputs are active), the pwm output signals are driven into their inactive states. the selection of which pwm outputs are deactivated (while in the fault state) is determined by the fltcon bit in the fltconfig register as follows:  fltcon = 1 . when flta or fltb is asserted, the pwm outputs (i.e., pwm[7:0]) are driven into their inactive state  fltcon = 0 . when flta or fltb is asserted, only pwm[5:0] outputs are driven inactive, leaving pwm[7:6] activated. 17.12.4 pwm outputs in debug mode the brfen bit in the fltconfig register controls the simulation of fault condition when a breakpoint is hit, while debugging the application using a in-circuit emulator (ice) or a in-circuit debugger (icd). setting the brfen to high, enables the fault condition on breakpoint, thus driving the pwm outputs to inactive state. this is done to avoid any continuous keeping of status on the pwm pin, which may result in damage of the power devices connected to the pwm outputs. if brfen = 0 , the fault condition on breakpoint is disabled. note: disabling only three pwm channels and leaving one pwm channel enabled when in the fault state, allows the flexibility to have at least one pwm channel enabled. none of the pwm outputs can be enabled (driven with the pwm duty cycle regis- ters) while fltcon = 1 and the fault con- dition is present. note: it is highly recommended to enable the fault condition on breakpoint if a debug- ging tool is used, while developing the firmware and the high-power circuitry is used. when the device is ready to pro- gram after debugging the firmware, brfen bit can be disabled.
pic18f2331/2431/4331/4431 ds39616b-page 208 preliminary ? 2003 microchip technology inc. register 17-8: fltconfig: fault configuration register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 brfen fltbs (1) fltbmod (1) fltben (1) fltcon fltas fltamod fltaen bit 7 bit 0 bit 7 brfen: breakpoint fault enable bit 1 = enable fault condition on a breakpoint (i.e., only when hdmin = 1 ) 0 = disable fault condition bit 6 fltbs: fault b status bit (1) 1 =fltb is asserted; if fltbmod = 0 , cleared by the user if fltbmod = 1 , cleared automatically at beginning of the new period when fltb is deasserted 0 = no fault bit 5 fltbmod: fault b mode bit (1) 1 = cycle-by-cycle mode: pins are inactive for the remainder of the current pwm period, or until fltb is deasserted. fltbs is cleared automatically when fltb is inactive (no fault present). 0 = inactive mode: pins are deactivated (catastrophic failure) until fltb is deasserted and fltbs is cleared by the user only. bit 4 fltben: fault b enable bit (1) 1 = enable fault b 0 = disable fault b bit 3 fltcon: fault configuration bit 1 =flta , fltb or both deactivates all pwm outputs 0 =flta or fltb deactivates pwm[5:0] bit 2 fltas: fault a status bit 1 =flta is asserted; if fltamod = 0 , cleared by the user if fltamod = 1 , cleared automatically at beginning of the new period when flta is deasserted. 0 = no fault bit 1 fltamod: fault a mode bit 1 = cycle-by-cycle mode: pins are inactive for the remainder of the current pwm period, or until flta is deasserted. fltas is cleared automatically. 0 = inactive mode: pins are deactivated (catastrophic failure) until flta is deasserted and fltas is cleared by the user only. bit 0 fltaen: fault a enable bit 1 = enable fault a 0 = disable fault a note 1: unimplemented in pic18f2x31 devices; maintain these bits clear. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 209 pic18f2331/2431/4331/4431 17.13 pwm update lockout for a complex pwm application, the user may need to write up to four duty cycle registers and the time base period register, ptper, at a given time. in some applications, it is important that all buffer registers be written before the new duty cycle and period values are loaded for use by the module. a pwm update lockout feature may optionally be enabled so the user may specify when new duty cycle buffer values are valid. the pwm update lockout feature is enabled by setting the control bit udis in the pwmcon1 register. this bit affects all duty cycle buffer registers and the pwm time base period buffer, ptper. to perform a pwm update lockout: 1. set the udis bit. 2. write all duty cycle registers and ptper, if applicable. 3. clear the udis bit to re-enable updates. 4. with this, when udis bit is cleared, the buffer values will be loaded to the actual registers. this makes a synchronous loading of the registers. 17.14 pwm special event trigger the pwm module has a special event trigger capability that allows a/d conversions to be synchronized to the pwm time base. the a/d sampling and conversion time may be programmed to occur at any point within the pwm period. the special event trigger allows the user to minimize the delay between the time when a/d conversion results are acquired and the time when the duty cycle value is updated. the pwm 16-bit special event trigger register sevtcmp (high and low), and five control bits in pwmcon1 register are used to control its operation. the ptmr value for which a special event trigger should occur is loaded into the sevtcmp register pair. sevtdir bit in pwmcon1 register specifies the counting phase when the pwm time base is in an up/down counting mode. if the sevtdir bit is cleared, the special event trigger will occur on the upward counting cycle of the pwm time base. if sevtdir is set, the special event trigger will occur on the downward count cycle of the pwm time base. the sevtdir bit has effect only when pwm timer is in the up/down counting mode. 17.14.1 special event trigger enable the pwm module will always produce special event trigger pulses. this signal may optionally be used by the a/d module. refer to chapter 20.0 "10-bit high-speed analog-to-digital converter (a/d) module" for details. 17.14.2 special event trigger postscaler the pwm special event trigger has a postscaler that allows a 1:1 to 1:16 postscale ratio. the postscaler is configured by writing the sevops3:sevops0 control bits in the pwmcon1 register. the special event output postscaler is cleared on any write to the sevtcmp register pair, or on any device reset.
pic18f2331/2431/4331/4431 ds39616b-page 210 preliminary ? 2003 microchip technology inc. table 17-6: registers associated with the power control pwm module name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u ipr3 ? ? ?ptip ic3drip ic2qeip ic1ip tmr5ip ---1 1111 ---1 1111 pie3 ? ? ?ptie ic3drie ic2qeie ic1ie tmr5ie ---0 0000 ---0 0000 pir3 ? ? ?ptif ic3drif ic2qeif ic1if tmr5if ---0 0000 ---0 0000 ptcon0 ptops3 ptops2 ptops1 ptops0 ptckps1 ptckps0 ptmod1 ptmod0 0000 0000 0000 0000 ptcon1 pten ptdir ? ? ? ? ? ? 00-- ---- 00-- ---- ptmrl (1) pwm time base (lower 8 bits) 0000 0000 0000 0000 ptmrh (1) ? ? ? ? pwm time base (upper 4 bits) ---- 0000 ---- 0000 ptperl (1) pwm time base period (lower 8 bits) 1111 1111 1111 1111 ptperh (1) ? ? ? ? pwm time base period (upper 4 bits) ---- 1111 ---- 1111 sevtcmpl (1) pwm special event compare (lower 8 bits) 0000 0000 0000 0000 sevtcmph (1) ? ? ? ? pwm special event compare (upper 4 bits) ---- 0000 ---- 0000 pwmcon0 ? pwmen2 pwmen1 pwmen0 pmod3 (2) pmod2 pmod1 pmod0 -101 0000 -101 0000 pwmcon1 sevops3 sevops2 sevops1 sevops0 sevtdir ?udisosync 0000 0-00 0000 0-00 dtcon dtps1 dtps0 dead time a value register 0000 0000 0000 0000 fltconfig brfen fltbs (2) fltbmod (2) fltben (2) fltcon fltas fltamod fltaen 0000 0000 0000 0000 ovdcond povd7 (2) povd6 (2) povd5 povd4 povd3 povd2 povd1 povd0 1111 1111 1111 1111 ovdcons pout7 (2) pout6 (2) pout5 pout4 pout3 pout2 pout1 pout0 0000 0000 0000 0000 pdc0l (1) pwm duty cycle #0l register (lower 8 bits) --00 0000 --00 0000 pdc0h (1) ? ? pwm duty cycle #0h register (upper 6 bits) 0000 0000 0000 0000 pdc1l (1) pwm duty cycle #1l register (lower 8 bits) 0000 0000 0000 0000 pdc1h (1) ? ? pwm duty cycle #1h register (upper 6 bits) --00 0000 --00 0000 pdc2l (1) pwm duty cycle #2l register (lower 8 bits) 0000 0000 0000 0000 pdc2h (1) ? ? pwm duty cycle #2h register (upper 6 bits) --00 0000 --00 0000 pdc3l (1,2) pwm duty cycle #3l register (lower 8 bits) 0000 0000 0000 0000 pdc3h (1,2) ? ? pwm duty cycle #3h register (upper 6 bits) --00 0000 --00 0000 legend: - = unimplemented, u = unchanged. shaded cells are not used with the power control pwm. note 1: double-buffered register pairs. refer to text for explana tion of how these registers are read and written to. 2: unimplemented in pic18f2x31 devices; maintain these bits clear. reset values shown are for pic18f4x31 devices.
? 2003 microchip technology inc. preliminary ds39616b-page 211 pic18f2331/2431/4331/4431 18.0 synchronous serial port (ssp) module 18.1 ssp module overview the synchronous serial port (ssp) module is a serial interface useful for communicating with other periph- eral or microcontroller devices. these peripheral devices may be serial eeproms, shift registers, dis- play drivers, a/d converters, etc. the ssp module can operate in one of two modes:  serial peripheral interface (spi?)  inter-integrated circuit (i 2 c?) an overview of i 2 c operations and additional informa- tion on the ssp module can be found in the picmicro ? mid-range mcu family reference manual (ds33023). refer to application note an578, ? use of the ssp module in the i 2 c? multi-master environment ? (ds00578). 18.2 spi mode this section contains register definitions and opera- tional characteristics of the spi module. additional information on the spi module can be found in the picmicro ? mid-range mcu family reference manual (ds33023a). spi mode allows 8 bits of data to be synchronously transmitted and received simultaneously. to accom- plish communication, typically three pins are used:  serial data out (sdo) ? rc7/rx/dt/sdo  serial data in (sdi) ? rc4/int1/sdi/sda  serial clock (sck) ? rc5/int2/sck/scl additionally, a fourth pin may be used when in a slave mode of operation:  slave select (ss ) ? rc6/tx/ck/ss when initializing the spi, several options need to be specified. this is done by programming the appropriate control bits in the sspcon register (sspcon<5:0>) and sspstat<7:6>. these control bits allow the following to be specified:  master mode (sck is the clock output)  slave mode (sck is the clock input)  clock polarity (idle state of sck)  clock edge (output data on rising/falling edge of sck)  clock rate (master mode only)  slave select mode (slave mode only)
pic18f2331/2431/4331/4431 ds39616b-page 212 preliminary ? 2003 microchip technology inc. register 18-1: sspstat: sync serial port status register (address 94h) r/w-0 r/w-0 r-0 r-0 r-0 r-0 r-0 r-0 smp cke d/a psr/w ua bf bit 7 bit 0 bit 7 smp: spi data input sample phase bit spi master mode: 1 = input data sampled at end of data output time 0 = input data sampled at middle of data output time (microwire ? ) spi slave mode: smp must be cleared when spi is used in slave mode i 2 c mode: this bit must be maintained clear bit 6 cke : spi clock edge select bit (figure 18-2, figure 18-3, and figure 18-4) spi mode, ckp = 0 : 1 = data transmitted on rising edge of sck (microwire ? alternate) 0 = data transmitted on falling edge of sck spi mode, ckp = 1 : 1 = data transmitted on falling edge of sck (microwire ? default) 0 = data transmitted on rising edge of sck i 2 c mode: this bit must be maintained clear bit 5 d/a : data/address bit (i 2 c mode only) 1 = indicates that the last byte received or transmitted was data 0 = indicates that the last byte received or transmitted was address bit 4 p : stop bit (i 2 c mode only) this bit is cleared when the ssp module is disabled, or when the start bit is detected last. sspen is cleared. 1 = indicates that a stop bit has been detected last (this bit is ? 0 ? on reset) 0 = stop bit was not detected last bit 3 s : start bit (i 2 c mode only) this bit is cleared when the ssp module is disabled, or when the stop bit is detected last. sspen is cleared. 1 = indicates that a start bit has been detected last (this bit is ? 0 ? on reset) 0 = start bit was not detected last bit 2 r/w : read/write bit information (i 2 c mode only) this bit holds the r/w bit information following the last address match. this bit is only valid from the address match to the next start bit, stop bit, or ack bit. 1 = read 0 = write bit 1 ua : update address bit (10-bit i 2 c mode only) 1 = indicates that the user needs to update the address in the sspadd register 0 = address does not need to be updated bit 0 bf : buffer full status bit receive (spi and i 2 c modes): 1 = receive complete, sspbuf is full 0 = receive not complete, sspbuf is empty transmit (i 2 c mode only): 1 = transmit in progress, sspbuf is full 0 = transmit complete, sspbuf is empty legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por reset ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 213 pic18f2331/2431/4331/4431 register 18-2: sspcon: sync serial port control register (address 14h) r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 bit 7 bit 0 bit 7 wcol : write collision detect bit 1 = the sspbuf register is written while it is still transmitting the previous word (must be cleared in software) 0 = no collision bit 6 sspov : receive overflow indicator bit in spi mode: 1 = a new byte is received, while the sspbuf register is still holding the previous data. in case of overflow, the data in sspsr is lost. overflow can only occur in slave mode. the user must read the sspbuf, even if only transmitting data, to avoid setting overflow. in master mode, the overflow bit is not set since each new reception (and transmission) is initiated by writing to the sspbuf register. 0 = no overflow in i 2 c mode: 1 = a byte is received while the sspbuf register is still holding the previous byte. sspov is a ?don?t care? in transmit mode. sspov must be cleared in software in either mode. 0 = no overflow bit 5 sspen : synchronous serial port enable bit in spi mode: 1 = enables serial port and configures sck, sdo and sdi as serial port pins 0 = disables serial port and configures these pins as i/o port pins in i 2 c mode: 1 = enables the serial port and configures the sda and scl pins as serial port pins 0 = disables serial port and configures these pins as i/o port pins in both modes, when enabled, these pins must be properly configured as input or output. bit 4 ckp : clock polarity select bit in spi mode: 1 = idle state for clock is a high level (microwire ? default) 0 = idle state for clock is a low level (microwire ? alternate) in i 2 c mode: sck release control 1 = enable clock 0 = holds clock low (clock stretch). (used to ensure data setup time.) bit 3-0 sspm3:sspm0 : synchronous serial port mode select bits 0000 = spi master mode, clock = f osc /4 0001 = spi master mode, clock = f osc /16 0010 = spi master mode, clock = f osc /64 0011 = spi master mode, clock = tmr2 output/2 0100 = spi slave mode, clock = sck pin. ss pin control enabled. 0101 = spi slave mode, clock = sck pin. ss pin control disabled. ss can be used as i/o pin. 0110 = i 2 c slave mode, 7-bit address 0111 = i 2 c slave mode, 10-bit address 1011 = i 2 c firmware controlled master mode (slave idle) 1110 = i 2 c slave mode, 7-bit address with start and stop bit interrupts enabled 1111 = i 2 c slave mode, 10-bit address with start and stop bit interrupts enabled legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por reset ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 214 preliminary ? 2003 microchip technology inc. figure 18-1: ssp block diagram (spi mode) to enable the serial port, ssp enable bit sspen (sspcon<5>) must be set. to reset or reconfigure spi mode, clear bit sspen, reinitialize the sspcon register, and then set bit sspen. this configures the sdi, sdo, sck, and ss pins as serial port pins. for the pins to behave as the serial port function, they must have their data direction bits (in the trisc register) appropriately programmed. that is:  sdi must have trisc<4> set  sdo must have trisc<5> cleared  sck (master mode) must have trisc<3> cleared  sck (slave mode) must have trisc<3> set ss must have trisa<5> set and adcon must be configured such that ra5 is a digital i/o . read write internal data bus rc4/sdi/sda rc5/sdo ra5/ss/an4 rc3/sck/ sspsr reg sspbuf reg sspm3:sspm0 bit0 shift clock ss control enable edge select clock select tmr2 output t cy prescaler 4, 16, 64 trisc<3> 2 edge select 2 4 scl peripheral oe note 1: when the spi is in slave mode with ss pin control enabled (sspcon<3:0> = 0100 ), the spi module will reset if the ss pin is set to v dd . 2: if the spi is used in slave mode with cke = 1 , then the ss pin control must be enabled. 3: when the spi is in slave mode with ss pin control enabled (sspcon<3:0> = 0100 ), the state of the ss pin can affect the state read back from the trisc<5> bit. the peripheral oe signal from the ssp module into portc controls the state that is read back from the trisc<5> bit (see section 10.3 ?portc, trisc and latc registers? for information on portc). if read- modify-write instructions, such as bsf, are performed on the trisc register while the ss pin is high, this will cause the trisc<5> bit to be set, thus disabling the sdo output.
? 2003 microchip technology inc. preliminary ds39616b-page 215 pic18f2331/2431/4331/4431 figure 18-2: spi mode timing, master mode figure 18-3: spi mode timing (slave mode with cke = 0) sck (ckp = 0 , sdi (smp = 0 ) sspif bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 sdi (smp = 1 ) sck (ckp = 0 , sck (ckp = 1 , sck (ckp = 1 , sdo bit7 bit7 bit0 bit0 cke = 0 ) cke = 1 ) cke = 0 ) cke = 1 ) sck (ckp = 0 ) sdi (smp = 0 ) sspif bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 sck (ckp = 1 ) sdo bit7 bit0 ss (optional)
pic18f2331/2431/4331/4431 ds39616b-page 216 preliminary ? 2003 microchip technology inc. figure 18-4: spi mode timing (s lave mode with cke = 1) table 18-1: registers associated with spi operation sck (ckp = 0 ) sdi (smp = 0 ) sspif bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 sck (ckp = 1 ) sdo bit7 bit0 ss name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets intcon gie peie tmr0ie inte rbie tmr0if intf rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ccp1if tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ccp1ie tmr2ie tmr1ie 0000 0000 0000 0000 trisc portc data direction register 1111 1111 1111 1111 sspbuf synchronous serial port receive buffer/transmit register xxxx xxxx uuuu uuuu sspcon wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 0000 0000 0000 0000 trisa ? ? porta data direction register --11 1111 --11 1111 sspstat smp cke d/a p s r/w ua bf 0000 0000 0000 0000 legend: x = unknown, u = unchanged, ? = unimplemented, read as ? 0 ?. shaded cells are not used by the ssp in spi mode. note 1: bits pspie and pspif are reserved on the pic16f73/76; always maintain these bits clear.
? 2003 microchip technology inc. preliminary ds39616b-page 217 pic18f2331/2431/4331/4431 18.3 ssp i 2 c operation the ssp module in i 2 c mode, fully implements all slave functions, except general call support, and provides interrupts on start and stop bits in hardware to facilitate firmware implementations of the master functions. the ssp module implements the standard mode specifications, as well as 7-bit and 10-bit addressing. two pins are used for data transfer. these are the sck/ scl pin, which is the clock (scl), and the sdi/sda pin, which is the data (sda). the user must configure these pins as inputs or outputs through the trisc<5:4> or trisd<3:2> bits. the ssp module functions are enabled by setting ssp enable bit sspen (sspcon<5>). figure 18-5: ssp block diagram (i 2 c mode) the ssp module has five registers for i 2 c operation. these are the:  ssp control register (sspcon)  ssp status register (sspstat)  serial receive/transmit buffer (sspbuf)  ssp shift register (sspsr) ? not directly accessible  ssp address register (sspadd) the sspcon register allows control of the i 2 c opera- tion. four mode selection bits (sspcon<3:0>) allow one of the following i 2 c modes to be selected: i 2 c slave mode (7-bit address) i 2 c slave mode (10-bit address) i 2 c slave mode (7-bit address), with start and stop bit interrupts enabled to support firmware master mode i 2 c slave mode (10-bit address), with start and stop bit interrupts enabled to support firmware master mode i 2 c start and stop bit interrupts enabled to sup- port firmware master mode; slave is idle selection of any i 2 c mode with the sspen bit set, forces the scl and sda pins to be open-drain, provided these pins are programmed to inputs by setting the appropriate trisc or trisd bits. pull-up resistors must be provided externally to the scl and sda pins for proper operation of the i 2 c module. additional information on ssp i 2 c operation can be found in the picmicro ? mid-range mcu family reference manual (ds33023a). 18.3.1 slave mode in slave mode, the scl and sda pins must be config- ured as inputs (trisc<5:4> or trisd<3:2> set). the ssp module will override the input state with the output data when required (slave-transmitter). when an address is matched, or the data transfer after an address match is received, the hardware automati- cally will generate the acknowledge (ack ) pulse, and then load the sspbuf register with the received value currently in the sspsr register. there are certain conditions that will cause the ssp module not to give this ack pulse. they include (either or both): a) the buffer full bit bf (sspstat<0>) was set before the transfer was received. b) the overflow bit sspov (sspcon<6>) was set before the transfer was received. in this case, the sspsr register value is not loaded into the sspbuf, but bit sspif (pir1<3>) is set. table 18-2 shows what happens when a data transfer byte is received, given the status of bits bf and sspov. the shaded cells show the condition where user software did not properly clear the overflow condition. flag bit bf is cleared by reading the sspbuf register, while bit sspov is cleared through software. the scl clock input must have a minimum high and low for proper operation. the high and low times of the i 2 c specification, as well as the requirements of the ssp module, are shown in timing parameter #100 and parameter #101. read write sspsr reg match detect sspadd reg start and stop bit detect sspbuf reg internal data bus addr match set, reset s, p bits (sspstat reg ) sck/scl (1) shift clock msb sdi/ lsb sda (1) note 1: when sspmx = 1 in config3h: sck/scl is multiplexed to pin rc5, sda/sdi is multiplexed to pin rc4, and sdo is multiplexed to pin rc7. when sspmx = 0 in config3h: sck/scl is multiplexed to pin rd3, sda/sdi is multiplexed to pin rd2, and sdo is multiplexed to pin rd1.
pic18f2331/2431/4331/4431 ds39616b-page 218 preliminary ? 2003 microchip technology inc. 18.3.1.1 addressing once the ssp module has been enabled, it waits for a start condition to occur. following the start condition, the 8-bits are shifted into the sspsr register. all incoming bits are sampled with the rising edge of the clock (scl) line. the value of register sspsr<7:1> is compared to the value of the sspadd register. the address is compared on the falling edge of the eighth clock (scl) pulse. if the addresses match, and the bf and sspov bits are clear, the following events occur: a) the sspsr register value is loaded into the sspbuf register. b) the buffer full bit, bf is set. c) an ack pulse is generated. d) ssp interrupt flag bit, sspif (pir1<3>), is set (interrupt is generated if enabled) on the falling edge of the ninth scl pulse. in 10-bit address mode, two address bytes need to be received by the slave (figure 18-7). the five most significant bits (msbs) of the first address byte specify if this is a 10-bit address. bit r/w (sspstat<2>) must specify a write so the slave device will receive the second address byte. for a 10-bit address, the first byte would equal ? 1111 0 a9 a8 0 ?, where a9 and a8 are the two msbs of the address. the sequence of events for 10-bit address is as follows, with steps 7-9 for slave-transmitter: 1. receive first (high) byte of address (bits sspif, bf, and bit ua (sspstat<1>) are set). 2. update the sspadd register with second (low) byte of address (clears bit ua and releases the scl line). 3. read the sspbuf register (clears bit bf) and clear flag bit sspif. 4. receive second (low) byte of address (bits sspif, bf and ua are set). 5. update the sspadd register with the first (high) byte of address. if match releases scl line, this will clear bit ua. 6. read the sspbuf register (clears bit bf) and clear flag bit sspif. 7. receive repeated start condition. 8. receive first (high) byte of address (bits sspif and bf are set). 9. read the sspbuf register (clears bit bf) and clear flag bit sspif. table 18-2: data transfer received byte actions 18.3.1.2 reception when the r/w bit of the address byte is clear and an address match occurs, the r/w bit of the sspstat register is cleared. the received address is loaded into the sspbuf register. when the address byte overflow condition exists, then no acknowledge (ack ) pulse is given. an overflow condition is defined as either bit bf (sspstat<0>) is set, or bit sspov (sspcon<6>) is set. this is an error condition due to the user?s firmware. an ssp interrupt is generated for each data transfer byte. flag bit, sspif (pir1<3>), must be cleared in software. the sspstat register is used to determine the status of the byte. status bits as data transfer is received sspsr sspbuf generate ack pulse set bit sspif (ssp interrupt occurs if enabled) bf sspov 00 yes yes yes 10 no no yes 11 no no yes 0 1 no no yes note: shaded cells show the conditions where the user software did not properly clear the overflow condition.
? 2003 microchip technology inc. preliminary ds39616b-page 219 pic18f2331/2431/4331/4431 figure 18-6: i 2 c waveforms for reception (7-bit address) 18.3.1.3 transmission when the r/w bit of the incoming address byte is set and an address match occurs, the r/w bit of the sspstat register is set. the received address is loaded into the sspbuf register. the ack pulse will be sent on the ninth bit, and pin sck/scl is held low. the transmit data must be loaded into the sspbuf register, which also loads the sspsr register. then, pin sck/scl should be enabled by setting bit ckp (sspcon<4>). the master must monitor the scl pin prior to asserting another clock pulse. the slave devices may be holding off the master by stretching the clock. the eight data bits are shifted out on the falling edge of the scl input. this ensures that the sda signal is valid during the scl high time (figure 18-7). an ssp interrupt is generated for each data transfer byte. flag bit sspif must be cleared in software, and the sspstat register is used to determine the status of the byte. flag bit sspif is set on the falling edge of the ninth clock pulse. as a slave-transmitter, the ack pulse from the master- receiver is latched on the rising edge of the ninth scl input pulse. if the sda line was high (not ack ), then the data transfer is complete. when the ack is latched by the slave, the slave logic is reset (resets sspstat register) and the slave then monitors for another occurrence of the start bit. if the sda line was low (ack ), the transmit data must be loaded into the sspbuf register, which also loads the sspsr register. then pin sck/scl should be enabled by setting bit ckp. figure 18-7: i 2 c waveforms for transmission (7-bit address) p 9 8 7 6 5 d0 d1 d2 d3 d4 d5 d6 d7 s a7 a6 a5 a4 a3 a2 a1 sda scl 12 3 4 5 6 7 8 9 12 3 4 56 7 89 123 4 bus master terminates transfer bit sspov is set because the sspbuf register is still full. cleared in software sspbuf register is read ack receiving data receiving data d0 d1 d2 d3 d4 d5 d6 d7 ack r/w = 0 receiving address sspif (pir1<3>) bf (sspstat<0>) sspov (sspcon<6>) ack ack is not sent. sda scl sspif (pir1<3>) bf (sspstat<0>) ckp (sspcon<4>) a7 a6 a5 a4 a3 a2 a1 ack d7 d6 d5 d4 d3 d2 d1 d0 ack transmitting data r/w = 1 receiving address 123456789 123456789 p cleared in software sspbuf is written in software from ssp interrupt service routine set bit after writing to sspbuf s data in sampled scl held low while cpu responds to sspif (the sspbuf must be written to before the ckp bit can be set)
pic18f2331/2431/4331/4431 ds39616b-page 220 preliminary ? 2003 microchip technology inc. 18.3.2 master mode master mode of operation is supported in firmware using interrupt generation on the detection of the start and stop conditions. the stop (p) and start (s) bits are cleared from a reset or when the ssp module is disabled. the stop (p) and start (s) bits will toggle based on the start and stop conditions. control of the i 2 c bus may be taken when the p bit is set, or the bus is idle and both the s and p bits are clear. in master mode, the scl and sda lines are manipu- lated by clearing the corresponding trisc<5:4> or trisd<3:2> bits. the output level is always low, irre- spective of the value(s) in portc<5:4> or portd<3:2>. so when transmitting data, a ' 1 ' data bit must have the trisc<4> bit set (input) and a ' 0 ' data bit must have the trisc<4> bit cleared (output). the same scenario is true for the scl line with the trisc<4> or trisd<2> bit. pull-up resistors must be provided externally to the scl and sda pins for proper operation of the i 2 c module. the following events will cause ssp interrupt flag bit, sspif, to be set (ssp interrupt will occur if enabled):  start condition  stop condition  data transfer byte transmitted/received master mode of operation can be done with either the slave mode idle (sspm3:sspm0 = 1011 ), or with the slave active. when both master and slave modes are enabled, the software needs to differentiate the source(s) of the interrupt. 18.3.3 multi-master mode in multi-master mode, the interrupt generation on the detection of the start and stop conditions, allows the determination of when the bus is free. the stop (p) and start (s) bits are cleared from a reset or when the ssp module is disabled. the stop (p) and start (s) bits will toggle based on the start and stop conditions. control of the i 2 c bus may be taken when bit p (sspstat<4>) is set, or the bus is idle and both the s and p bits clear. when the bus is busy, enabling the ssp interrupt will generate the interrupt when the stop condition occurs. in multi-master operation, the sda line must be moni- tored to see if the signal level is the expected output level. this check only needs to be done when a high level is output. if a high level is expected and a low level is present, the device needs to release the sda and scl lines (set trisc<5:4> or trisd<3:2> ). there are two stages where this arbitration can be lost, these are:  address transfer  data transfer when the slave logic is enabled, the slave continues to receive. if arbitration was lost during the address trans- fer stage, communication to the device may be in progress. if addressed, an ack pulse will be gener- ated. if arbitration was lost during the data transfer stage, the device will need to retransfer the data at a later time. table 18-3: registers associated with i 2 c operation name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on: por, bor value on all other resets intcon gie peie tmr0ie inte rbie tmr0if intf rbif 0000 000x 0000 000u pir1 pspif (1) adif rcif txif sspif ccp1if tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie (1) adie rcie txie sspie ccp1ie tmr2ie tmr1ie 0000 0000 0000 0000 sspbuf synchronous serial port receive buffer/transmit register xxxx xxxx uuuu uuuu sspadd synchronous serial port (i 2 c mode) address register 0000 0000 0000 0000 sspcon wcol sspov sspen ckp sspm3 sspm2 sspm1 sspm0 0000 0000 0000 0000 sspstat smp (2) cke (2) d/a psr/w ua bf 0000 0000 0000 0000 trisc (3) portc data direction register 1111 1111 1111 1111 trisd (3) portd data direction register 1111 1111 1111 1111 legend: x = unknown, u = unchanged, ? = unimplemented locations read as ? 0 ?. shaded cells are not used by ssp module in i 2 c mode. note 1: pspif and pspie are reserved on the pic16f73/76; always maintain these bits clear. 2: maintain these bits clear in i 2 c mode. 3: depending upon the setting of sspmx in config3h, these pins are multiplexed to portc or portd.
? 2003 microchip technology inc. preliminary ds39616b-page 221 pic18f2331/2431/4331/4431 19.0 enhanced universal synchronous asynchronous receiver transmitter (eusart) the universal synchronous asynchronous receiver transmitter (eusart) module is one of the two serial i/o modules available in the pic18f2331/2431/4331/ 4431 family of microcontrollers. eusart is also known as a serial communications interface or sci. the eusart can be configured as a full-duplex asynchronous system that can communicate with peripheral devices, such as crt terminals and personal computers. it can also be configured as a half- duplex synchronous system that can communicate with peripheral devices, such as a/d or d/a integrated circuits, serial eeproms, etc. the eusart module implements additional features, including automatic baud rate detection and calibration, automatic wake-up on sync break reception and 12-bit break character transmit. these make it ideally suited for use in local interconnect network (lin) bus systems. the usart can be configured in the following modes:  asynchronous (full-duplex) with: - auto-wake-up on character reception - auto-baud calibration - 12-bit break character transmission  synchronous ? master (half-duplex) with selectable clock polarity  synchronous ? slave (half-duplex) with selectable clock polarity in order to configure pins rc6/tx/ck/ss and rc7/rx/ dt/sdo as the universal synchronous asynchronous receiver transmitter:  spen (rcsta<7>) bit must be set ( = 1 ),  trisc<6> bit must be set ( = 1 ), and  trisc<1> bit must be set ( = 1 ). the operation of the enhanced usart module is controlled through three registers:  transmit status and control (txsta)  receive status and control (rcsta)  baud rate control (baudctl) these are detailed in on the following pages in register 19-1, register 19-2 and register 19-3, respectively. 19.1 asynchronous operation in power-managed modes the usart may operate in asynchronous mode, while the peripheral clocks are being provided by the internal oscillator block. this makes it possible to remove the crystal or resonator that is commonly connected as the primary clock on the osc1 and osc2 pins. the factory calibrates the internal oscillator block out- put (intosc) for 8 mhz (see table 25-6). however, this frequency may drift as v dd or temperature changes, and this directly affects the asynchronous baud rate. two methods may be used to adjust the baud rate clock, but both require a reference clock source of some kind. the first (preferred) method uses the osctune register to adjust the intosc output back to 8 mhz. adjusting the value in the osctune register allows for fine resolution changes to the system clock source (see section 3.6 ?intosc frequency drift? for more information). the other method adjusts the value in the baud rate generator. there may not be fine enough resolution when adjusting the baud rate generator to compen- sate for a gradual change in the peripheral clock frequency. note: the usart control will automatically reconfigure the pin from input to output as needed.
pic18f2331/2431/4331/4431 ds39616b-page 222 preliminary ? 2003 microchip technology inc. register 19-1: txsta: transmit status and control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r-1 r/w-0 csrc tx9 txen sync sendb brgh trmt tx9d bit 7 bit 0 bit 7 csrc: clock source select bit asynchronous mode: don?t care synchronous mode: 1 = master mode (clock generated internally from brg) 0 = slave mode (clock from external source) bit 6 tx9: 9-bit transmit enable bit 1 = selects 9-bit transmission 0 = selects 8-bit transmission bit 5 txen: transmit enable bit 1 = transmit enabled 0 = transmit disabled note: sren/cren overrides txen in sync mode. bit 4 sync: usart mode select bit 1 = synchronous mode 0 = asynchronous mode bit 3 sendb: send break character bit asynchronous mode: 1 = send sync break on next transmission (cleared by hardware upon completion) 0 = sync break transmission completed synchronous mode: don?t care bit 2 brgh: high baud rate select bit asynchronous mode: 1 = high speed 0 = low speed synchronous mode: unused in this mode bit 1 trmt: transmit shift register status bit 1 = tsr empty 0 = tsr full bit 0 tx9d: 9th bit of transmit data can be address/data bit or a parity bit. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 223 pic18f2331/2431/4331/4431 register 19-2: rcsta: receive status and control register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r-0 r-0 r-x spen rx9 sren cren adden ferr oerr rx9d bit 7 bit 0 bit 7 spen: serial port enable bit 1 = serial port enabled (configures rx/dt and tx/ck pins as serial port pins) 0 = serial port disabled (held in reset) bit 6 rx9: 9-bit receive enable bit 1 = selects 9-bit reception 0 = selects 8-bit reception bit 5 sren: single receive enable bit asynchronous mode : don?t care synchronous mode ? master: 1 = enables single receive 0 = disables single receive this bit is cleared after reception is complete. synchronous mode ? slave: don?t care bit 4 cren: continuous receive enable bit asynchronous mode: 1 = enables receiver 0 = disables receiver synchronous mode: 1 = enables continuous receive until enable bit cren is cleared (cren overrides sren) 0 = disables continuous receive bit 3 adden: address detect enable bit asynchronous mode 9-bit (rx9 = 1 ) : 1 = enables address detection, enable interrupt and load the receive buffer when rsr<8> is set 0 = disables address detection, all bytes are received and ninth bit can be used as parity bit asynchronous mode 8-bit (rx9 = 0 ) : don?t care bit 2 ferr: framing error bit 1 = framing error (can be updated by reading rcreg register and receive next valid byte) 0 = no framing error bit 1 oerr: overrun error bit 1 = overrun error (can be cleared by clearing bit cren) 0 = no overrun error bit 0 rx9d: 9th bit of received data this can be address/data bit or a parity bit and must be calculated by user firmware. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 224 preliminary ? 2003 microchip technology inc. register 19-3: baudctl: baud rate control re gister u-0 r-1 u-0 r/w-0 r/w-0 u-0 r/w-0 r/w-0 ? rcidl ? sckp brg16 ? wue abden bit 7 bit 0 bit 7 unimplemented: read as ? 0 ? bit 6 rcidl : receive operation idle status bit 1 = receiver is idle 0 = receive in progress bit 5 unimplemented: read as ? 0 ? bit 4 sckp : synchronous clock polarity select bit asynchronous mode: unused in this mode synchronous mode: 1 = idle state for clock (ck) is a high level 0 = idle state for clock (ck) is a low level bit 3 brg16: 16-bit baud rate register enable bit 1 = 16-bit baud rate generator ? spbrgh and spbrg 0 = 8-bit baud rate generator ? spbrg only (compatible mode), spbrgh value ignored bit 2 unimplemented: read as ? 0 ? bit 1 wue: wake-up enable bit asynchronous mode: 1 = usart will continue to sample the rx pin ? interrupt generated on falling edge; bit cleared in hardware on following rising edge 0 = rx pin not monitored or rising edge detected synchronous mode: unused in this mode bit 0 abden : auto-baud detect enable bit asynchronous mode: 1 = enable baud rate measurement on the next character ? requires reception of a sync field (55h); cleared in hardware upon completion 0 = baud rate measurement disabled or completed synchronous mode: unused in this mode legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 225 pic18f2331/2431/4331/4431 19.2 usart baud rate generator (brg) the brg is a dedicated 8-bit or 16-bit generator, that supports both the asynchronous and synchronous modes of the usart. by default, the brg operates in 8-bit mode; setting the brg16 bit (baudctl<3>) selects 16-bit mode. the spbrgh:spbrg register pair controls the period of a free running timer. in asynchronous mode, bits brgh (txsta<2>) and brg16 also control the baud rate. in synchronous mode, bit brgh is ignored. table 19-1 shows the formula for computation of the baud rate for different usart modes, which only apply in master mode (internally generated clock). given the desired baud rate and f osc , the nearest integer value for the spbrgh:spbrg registers can be calculated using the formulas in table 19-1. from this, the error in baud rate can be determined. an example calculation is shown in example 19-1. typical baud rates and error values for the various asynchronous modes are shown in table 19-2. it may be advanta- geous to use the high baud rate (brgh = 1 ), or the 16- bit brg to reduce the baud rate error, or achieve a slow baud rate for a fast oscillator frequency. writing a new value to the spbrgh:spbrg registers causes the brg timer to be reset (or cleared). this ensures the brg does not wait for a timer overflow before outputting the new baud rate. 19.2.1 power-managed mode operation the system clock is used to generate the desired baud rate; however, when a power-managed mode is entered, the clock source may be operating at a different frequency than in pri_run mode. in sleep mode, no clocks are present and in pri_idle, the primary clock source continues to provide clocks to the baud rate generator; however, in other power- managed modes, the clock frequency will probably change. this may require the value in spbrg to be adjusted. if the system clock is changed during an active receive operation, a receive error or data loss may result. to avoid this problem, check the status of the rcidl bit and make sure that the receive operation is idle before changing the system clock. 19.2.2 sampling the data on the rc7/rx/dt/sdo pin is sampled three times by a majority detect circuit to determine if a high or a low level is present at the rx pin. table 19-1: baud rate formulas configuration bits brg/usart mode baud rate formula sync brg16 brgh 000 8-bit/asynchronous f osc /[64 (n+1)] 001 8-bit/asynchronous f osc /[16 (n+1)] 010 16-bit/asynchronous 011 16-bit/asynchronous f osc /[4 (n+1)] 10x 8-bit/synchronous 11x 16-bit/synchronous legend: x = don?t care, n = value of spbrgh:spbrg register pair
pic18f2331/2431/4331/4431 ds39616b-page 226 preliminary ? 2003 microchip technology inc. example 19-1: calculating baud rate error table 19-2: registers associated with baud rate generator name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets txsta csrc tx9 txen sync sendb brgh trmt tx9d 0000 -010 0000 -010 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x baudctl ? rcidl ? sckp brg16 ? wue abden -1-1 0-00 -1-1 0-00 spbrgh baud rate generator register, high byte 0000 0000 0000 0000 spbrg baud rate generator register, low byte 0000 0000 0000 0000 legend: x = unknown, ? = unimplemented, read as ? 0 ?. shaded cells are not used by the brg. for a device with f osc of 16 mhz, desired baud rate of 9600, asynchronous mode, 8-bit brg: desired baud rate= f osc / (64 ([spbrgh:spbrg] + 1)) solving for spbrgh:spbrg: x = ((fosc / desired baud rate)/64) ? 1 = ((16000000 / 9600) / 64) ? 1 = [25.042] = 25 calculated baud rate=16000000 / (64 (25 + 1)) = 9615 error = (calculated baud rate ? desired baud rate) / desired baud rate = (9615 ? 9600) / 9600 = 0.16% table 19-3: baud rates for asynchronous modes baud rate (k) sync = 0, brgh = 0, brg16 = 0 f osc = 40.000 mhz f osc = 20.000 mhz f osc = 10.000 mhz f osc = 8.000 mhz actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) 0.3???????????? 1.2 ? ? ? 1.221 1.73 255 1.202 0.16 129 1201 -0.16 103 2.4 2.441 1.73 255 2.404 0.16 129 2.404 0.16 64 2403 -0.16 51 9.6 9.615 0.16 64 9.766 1.73 31 9.766 1.73 15 9615 -0.16 12 19.2 19.531 1.73 31 19.531 1.73 15 19.531 1.73 7 ? ? ? 57.6 56.818 -1.36 10 62.500 8.51 4 52.083 -9.58 2 ? ? ? 115.2 125.000 8.51 4 104.167 -9.58 2 78.125 -32.18 1 ? ? ? baud rate (k) sync = 0, brgh = 0, brg16 = 0 f osc = 4.000 mhz f osc = 2.000 mhz f osc = 1.000 mhz actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) 0.3 0.300 0.16 207 300 -0.16 103 300 -0.16 51 1.2 1.202 0.16 51 1201 -0.16 25 1201 -0.16 12 2.4 2.404 0.16 25 2403 -0.16 12 ? ? ? 9.6 8.929 -6.99 6 ? ? ? ? ? ? 19.2 20.833 8.51 2 ? ? ? ? ? ? 57.6 62.500 8.51 0 ? ? ? ? ? ? 115.2 62.500 -45.75 0 ? ? ? ? ? ?
? 2003 microchip technology inc. preliminary ds39616b-page 227 pic18f2331/2431/4331/4431 baud rate (k) sync = 0, brgh = 1, brg16 = 0 f osc = 40.000 mhz f osc = 20.000 mhz f osc = 10.000 mhz f osc = 8.000 mhz actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) 2.4 ? ? ? ? ? ? 2.441 1.73 255 2403 -0.16 207 9.6 9.766 1.73 255 9.615 0.16 129 9.615 0.16 64 9615 -0.16 51 19.2 19.231 0.16 129 19.231 0.16 64 19.531 1.73 31 19230 -0.16 25 57.6 58.140 0.94 42 56.818 -1.36 21 56.818 -1.36 10 55555 3.55 8 115.2 113.636 -1.36 21 113.636 -1.36 10 125.000 8.51 4 ? ? ? baud rate (k) sync = 0, brgh = 1, brg16 = 0 f osc = 4.000 mhz f osc = 2.000 mhz f osc = 1.000 mhz actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) 0.3 ? ? ? ? ? ? 300 -0.16 207 1.2 1.202 0.16 207 1201 -0.16 103 1201 -0.16 51 2.4 2.404 0.16 103 2403 -0.16 51 2403 -0.16 25 9.6 9.615 0.16 25 9615 -0.16 12 ? ? ? 19.2 19.231 0.16 12 ? ? ? ? ? ? 57.6 62.500 8.51 3 ? ? ? ? ? ? 115.2 125.000 8.51 1 ? ? ? ? ? ? baud rate (k) sync = 0, brgh = 0, brg16 = 1 f osc = 40.000 mhz f osc = 20.000 mhz f osc = 10.000 mhz f osc = 8.000 mhz actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) 0.3 0.300 0.00 8332 0.300 0.02 4165 0.300 0.02 2082 300 -0.04 1665 1.2 1.200 0.02 2082 1.200 -0.03 1041 1.200 -0.03 520 1201 -0.16 415 2.4 2.402 0.06 1040 2.399 -0.03 520 2.404 0.16 259 2403 -0.16 207 9.6 9.615 0.16 259 9.615 0.16 129 9.615 0.16 64 9615 -0.16 51 19.2 19.231 0.16 129 19.231 0.16 64 19.531 1.73 31 19230 -0.16 25 57.6 58.140 0.94 42 56.818 -1.36 21 56.818 -1.36 10 55555 3.55 8 115.2 113.636 -1.36 21 113.636 -1.36 10 125.000 8.51 4 ? ? ? baud rate (k) sync = 0, brgh = 0, brg16 = 1 f osc = 4.000 mhz f osc = 2.000 mhz f osc = 1.000 mhz actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) 0.3 0.300 0.04 832 300 -0.16 415 300 -0.16 207 1.2 1.202 0.16 207 1201 -0.16 103 1201 -0.16 51 2.4 2.404 0.16 103 2403 -0.16 51 2403 -0.16 25 9.6 9.615 0.16 25 9615 -0.16 12 ? ? ? 19.2 19.231 0.16 12 ? ? ? ? ? ? 57.6 62.500 8.51 3 ? ? ? ? ? ? 115.2 125.000 8.51 1 ? ? ? ? ? ? table 19-3: baud rates for asynchronous modes (continued)
pic18f2331/2431/4331/4431 ds39616b-page 228 preliminary ? 2003 microchip technology inc. baud rate (k) sync = 0, brgh = 1, brg16 = 1 or sync = 1, brg16 = 1 f osc = 40.000 mhz f osc = 20.000 mhz f osc = 10.000 mhz f osc = 8.000 mhz actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) 0.3 0.300 0.00 33332 0.300 0.00 16665 0.300 0.00 8332 300 -0.01 6665 1.2 1.200 0.00 8332 1.200 0.02 4165 1.200 0.02 2082 1200 -0.04 1665 2.4 2.400 0.02 4165 2.400 0.02 2082 2.402 0.06 1040 2400 -0.04 832 9.6 9.606 0.06 1040 9.596 -0.03 520 9.615 0.16 259 9615 -0.16 207 19.2 19.193 -0.03 520 19.231 0.16 259 19.231 0.16 129 19230 -0.16 103 57.6 57.803 0.35 172 57.471 -0.22 86 58.140 0.94 42 57142 0.79 34 115.2 114.943 -0.22 86 116.279 0.94 42 113.636 -1.36 21 117647 -2.12 16 baud rate (k) sync = 0, brgh = 1, brg16 = 1 or sync = 1, brg16 = 1 f osc = 4.000 mhz f osc = 2.000 mhz f osc = 1.000 mhz actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) actual rate (k) % error spbrg value (decimal) 0.3 0.300 0.01 3332 300 -0.04 1665 300 -0.04 832 1.2 1.200 0.04 832 1201 -0.16 415 1201 -0.16 207 2.4 2.404 0.16 415 2403 -0.16 207 2403 -0.16 103 9.6 9.615 0.16 103 9615 -0.16 51 9615 -0.16 25 19.2 19.231 0.16 51 19230 -0.16 25 19230 -0.16 12 57.6 58.824 2.12 16 55555 3.55 8 ? ? ? 115.2 111.111 -3.55 8 ? ? ? ? ? ? table 19-3: baud rates for asynchronous modes (continued)
? 2003 microchip technology inc. preliminary ds39616b-page 229 pic18f2331/2431/4331/4431 19.2.3 auto-baud rate detect the enhanced usart module supports the automatic detection and calibration of baud rate. this feature is active only in asynchronous mode and while the wue bit is clear. the automatic baud rate measurement sequence (figure 19-1) begins whenever a start bit is received and the abden bit is set. the calculation is self- averaging. in the auto-baud rate detect (abd) mode, the clock to the brg is reversed. rather than the brg clocking the incoming rx signal, the rx signal is timing the brg. in abd mode, the internal baud rate generator is used as a counter to time the bit period of the incoming serial byte stream. once the abden bit is set, the state machine will clear the brg and look for a start bit. the auto-baud detect must receive a byte with the value 55h (ascii ?u?, which is also the lin bus sync character), in order to calculate the proper bit rate. the measurement takes over both a low and a high bit time in order to minimize any effects caused by asymmetry of the incoming signal. after a start bit, the spbrg begins counting up using the preselected clock source on the first rising edge of rx. after eight bits on the rx pin, or the fifth rising edge, an accumulated value totalling the proper brg period is left in the spbrgh:spbrg registers. once the 5th edge is seen (should correspond to the stop bit), the abden bit is automatically cleared. while calibrating the baud rate period, the brg regis- ters are clocked at 1/8th the pre-configured clock rate. note that the brg clock will be configured by the brg16 and brgh bits. independent of the brg16 bit setting, both the spbrg and spbrgh will be used as a 16-bit counter. this allows the user to verify that no carry occurred for 8-bit modes, by checking for 00h in the spbrgh register. refer to table 19-4 for counter clock rates to the brg. while the abd sequence takes place, the usart state machine is held in idle. the rcif interrupt is set once the fifth rising edge on rx is detected. the value in the rcreg needs to be read to clear the rcif interrupt. rcreg content should be discarded. table 19-4: brg counter clock rates figure 19-1: automatic baud rate calculation note 1: if the wue bit is set with the abden bit, auto-baud rate detection will occur on the byte following the break character (see section 19.3.4 ?auto-wake-up on sync break character? ). 2: it is up to the user to determine that the incoming character baud rate is within the range of the selected brg clock source. some combinations of oscillator fre- quency and usart baud rates are not possible due to bit error rates. overall system timing and communication baud rates must be taken into consideration when using the auto-baud rate detec- tion feature. brg16 brgh brg counter clock 00 f osc /512 01 f osc /256 10 f osc /128 11 f osc /32 note: during the abd sequence, spbrg and spbrgh are both used as a 16-bit counter, independent of brg16 setting. brg value rx pin abden bit rcif bit bit 0 bit 1 (interrupt) read rcreg brg clock start auto-cleared set by user xxxxh 0000h edge #1 bit 2 bit 3 edge #2 bit 4 bit 5 edge #3 bit 6 bit 7 edge #4 stop bit edge #5 001ch note 1: the abd sequence requires the usart module to be configured in asynchronous mode and wue = 0 . spbrg xxxxh 1ch spbrgh xxxxh 00h
pic18f2331/2431/4331/4431 ds39616b-page 230 preliminary ? 2003 microchip technology inc. 19.3 usart asynchronous mode the asynchronous mode of operation is selected by clearing the sync bit (txsta<4>). in this mode, the usart uses standard non-return-to-zero (nrz) format (one start bit, eight or nine data bits and one stop bit). the most common data format is 8 bits. an on-chip dedicated 8-bit/16-bit baud rate generator can be used to derive standard baud rate frequencies from the oscillator. the usart transmits and receives the lsb first. the usart?s transmitter and receiver are functionally inde- pendent, but use the same data format and baud rate. the baud rate generator produces a clock, either x16 or x64 of the bit shift rate, depending on the brgh and brg16 bits (txsta<2> and baudctl<3>). parity is not supported by the hardware, but can be implemented in software and stored as the 9th data bit. asynchronous mode is available in all low-power modes; it is available in sleep mode only when auto- wake-up on sync break is enabled. when in pri_idle mode, no changes to the baud rate generator values are required; however, other low-power mode clocks may operate at another frequency than the primary clock. therefore, the baud rate generator values may need to be adjusted. when operating in asynchronous mode, the usart module consists of the following important elements:  baud rate generator  sampling circuit  asynchronous transmitter  asynchronous receiver  auto-wake-up on sync break character  12-bit break character transmit  auto-baud rate detection 19.3.1 usart asynchronous transmitter the usart transmitter block diagram is shown in figure 19-2. the heart of the transmitter is the transmit (serial) shift register (tsr). the shift register obtains its data from the read/write transmit buffer, txreg. the txreg register is loaded with data in software. the tsr register is not loaded until the stop bit has been transmitted from the previous load. as soon as the stop bit is transmitted, the tsr is loaded with new data from the txreg register (if available). once the txreg register transfers the data to the tsr register (occurs in one t cy ), the txreg register is empty and flag bit txif (pir1<4>) is set. this interrupt can be enabled/disabled by setting/clearing enable bit txie (pie1<4>). flag bit txif will be set, regardless of the state of enable bit txie and cannot be cleared in software. flag bit txif is not cleared immediately upon loading the transmit buffer register txreg. txif becomes valid in the second instruction cycle following the load instruction. polling txif immediately following a load of txreg will return invalid results. while flag bit txif indicates the status of the txreg register, another bit, trmt (txsta<1>), shows the status of the tsr register. status bit trmt is a read- only bit, which is set when the tsr register is empty. no interrupt logic is tied to this bit, so the user has to poll this bit in order to determine if the tsr register is empty. to set up an asynchronous transmission: 1. initialize the spbrgh:spbrg registers for the appropriate baud rate. set or clear the brgh and brg16 bits, as required, to achieve the desired baud rate. 2. enable the asynchronous serial port by clearing bit sync and setting bit spen. 3. if interrupts are desired, set enable bit txie. 4. if 9-bit transmission is desired, set transmit bit tx9. can be used as address/data bit. 5. enable the transmission by setting bit txen, which will also set bit txif. 6. if 9-bit transmission is selected, the ninth bit should be loaded in bit tx9d. 7. load data to the txreg register (starts transmission). if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. note 1: the tsr register is not mapped in data memory, so it is not available to the user. 2: flag bit txif is set when enable bit txen is set.
? 2003 microchip technology inc. preliminary ds39616b-page 231 pic18f2331/2431/4331/4431 figure 19-2: usart transmit block diagram figure 19-3: asynchronous transmission figure 19-4: asynchronous transmission (back to back) txif txie interrupt txen baud rate clk spbrg baud rate generator tx9d msb lsb data bus txreg register tsr register (8) 0 tx9 trmt spen rc6/tx/ck/ss pin pin buffer and control 8 ? ? ? spbrgh brg16 word 1 stop bit word 1 transmit shift reg start bit bit 0 bit 1 bit 7/8 write to txreg word 1 brg output (shift clock) rc6/tx/ck/ss txif bit (transmit buffer reg. empty flag) trmt bit (transmit shift reg. empty flag) 1 t cy (pin) transmit shift reg. write to txreg brg output (shift clock) rc6/tx/ck/ss txif bit (interrupt reg. flag) trmt bit (transmit shift reg. empty flag) word 1 word 2 word 1 word 2 start bit stop bit start bit transmit shift reg. word 1 word 2 bit 0 bit 1 bit 7/8 bit 0 note: this timing diagram shows two consecutive transmissions. 1 t cy 1 t cy (pin)
pic18f2331/2431/4331/4431 ds39616b-page 232 preliminary ? 2003 microchip technology inc. table 19-5: registers associated with asynchronous transmission name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 ? adif rcif txif ? ccp1if tmr2if tmr1if -000 -000 -000 -000 pie1 ? adie rcie txie ? ccp1ie tmr2ie tmr1ie -000 -000 -000 -000 ipr1 ? adip rcip txip ? ccp1ip tmr2ip tmr1ip -111 -111 -111 -111 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x txreg usart transmit register 0000 0000 0000 0000 txsta csrc tx9 txen sync sendb brgh trmt tx9d 0000 0010 0000 0010 baudctl ? rcidl ? sckp brg16 ? wue abden -1-1 0-00 -1-1 0-00 spbrgh baud rate generator register, high byte 0000 0000 0000 0000 spbrg baud rate generator register, low byte 0000 0000 0000 0000 legend: x = unknown, ? = unimplemented locations read as ? 0 ?. shaded cells are not used for asynchronous transmission.
? 2003 microchip technology inc. preliminary ds39616b-page 233 pic18f2331/2431/4331/4431 19.3.2 usart asynchronous receiver the receiver block diagram is shown in figure 19-5. the data is received on the rc7/rx/dt/sdo pin and drives the data recovery block. the data recovery block is actually a high-speed shifter operating at x16 times the baud rate, whereas the main receive serial shifter operates at the bit rate or at f osc . this mode would typically be used in rs-232 systems. to set up an asynchronous reception: 1. initialize the spbrgh:spbrg registers for the appropriate baud rate. set or clear the brgh and brg16 bits, as required, to achieve the desired baud rate. 2. enable the asynchronous serial port by clearing bit sync and setting bit spen. 3. if interrupts are desired, set enable bit rcie. 4. if 9-bit reception is desired, set bit rx9. 5. enable the reception by setting bit cren. 6. flag bit rcif will be set when reception is com- plete and an interrupt will be generated if enable bit rcie was set. 7. read the rcsta register to get the 9th bit (if enabled) and determine if any error occurred during reception. 8. read the 8-bit received data by reading the rcreg register. 9. if any error occurred, clear the error by clearing enable bit cren. 10. if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. 19.3.3 setting up 9-bit mode with address detect this mode would typically be used in rs-485 systems. to set up an asynchronous reception with address detect enable: 1. initialize the spbrgh:spbrg registers for the appropriate baud rate. set or clear the brgh and brg16 bits, as required, to achieve the desired baud rate. 2. enable the asynchronous serial port by clearing the sync bit and setting the spen bit. 3. if interrupts are required, set the rcen bit and select the desired priority level with the rcip bit. 4. set the rx9 bit to enable 9-bit reception. 5. set the adden bit to enable address detect. 6. enable reception by setting the cren bit. 7. the rcif bit will be set when reception is com- plete. the interrupt will be acknowledged if the rcie and gie bits are set. 8. read the rcsta register to determine if any error occurred during reception, as well as read bit 9 of data (if applicable). 9. read rcreg to determine if the device is being addressed. 10. if any error occurred, clear the cren bit. 11. if the device has been addressed, clear the adden bit to allow all received data into the receive buffer and interrupt the cpu. figure 19-5: usart receive block diagram x64 baud rate clk baud rate generator rc7/rx/dt/sdo pin buffer and control spen data recovery cren oerr ferr rsr register msb lsb rx9d rcreg register fifo interrupt rcif rcie data bus 8 64 16 or stop start (8) 7 1 0 rx9 ? ? ? spbrg spbrgh brg16 or 4
pic18f2331/2431/4331/4431 ds39616b-page 234 preliminary ? 2003 microchip technology inc. to set up an asynchronous transmission: 1. initialize the spbrg register for the appropriate baud rate. if a high-speed baud rate is desired, set bit brgh (see section 19.2 ?usart baud rate generator (brg)? ). 2. enable the asynchronous serial port by clearing bit sync and setting bit spen. 3. if interrupts are desired, set enable bit txie. 4. if 9-bit transmission is desired, set transmit bit tx9. can be used as address/data bit. 5. enable the transmission by setting bit txen, which will also set bit txif. 6. if 9-bit transmission is selected, the ninth bit should be loaded in bit tx9d. 7. load data to the txreg register (starts transmission). if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. figure 19-6: asynchronous reception table 19-6: registers associated with asynchronous reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 ? adif rcif txif ? ccp1if tmr2if tmr1if -000 -000 -000 -000 pie1 ? adie rcie txie ? ccp1ie tmr2ie tmr1ie -000 -000 -000 -000 ipr1 ? adip rcip txip ? ccp1ip tmr2ip tmr1ip -111 -111 -111 -111 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x rcreg usart receive register 0000 0000 0000 0000 txsta csrc tx9 txen sync sendb brgh trmt tx9d 0000 0010 0000 0010 baudctl ? rcidl ? sckp brg16 ? wue abden -1-1 0-00 -1-1 0-00 spbrgh baud rate generator register, high byte 0000 0000 0000 0000 spbrg baud rate generator register, low byte 0000 0000 0000 0000 legend: x = unknown, ? = unimplemented locations read as ? 0 ?. shaded cells are not used for asynchronous reception. start bit bit7/8 bit1 bit0 bit7/8 bit0 stop bit start bit start bit bit7/8 stop bit rx (pin) reg rcv buffer reg rcv shift read rcv buffer reg rcreg rcif (interrupt flag) oerr bit cren word 1 rcreg word 2 rcreg stop bit note: this timing diagram shows three words appearing on the rx input. the rcreg (receive buffer) is read after the third word, causing the oerr (overrun) bit to be set.
? 2003 microchip technology inc. preliminary ds39616b-page 235 pic18f2331/2431/4331/4431 19.3.4 auto-wake-up on sync break character during sleep mode, all clocks to the usart are sus- pended. because of this, the baud rate generator is inactive and a proper byte reception cannot be per- formed. the auto-wake-up feature allows the control- ler to wake-up due to activity on the rx/dt line, while the usart is operating in asynchronous mode. the auto-wake-up feature is enabled by setting the wue bit (baudctl<1>). once set, the typical receive sequence on rx/dt is disabled, and the usart remains in an idle state, monitoring for a wake-up event independent of the cpu mode. a wake-up event con- sists of a high-to-low transition on the rx/dt line. (this coincides with the start of a sync break or a wake-up signal character for the lin protocol.) following a wake-up event, the module generates an rcif interrupt. the interrupt is generated synchro- nously to the q clocks in normal operating modes (figure 19-7), and asynchronously if the device is in sleep mode (figure 19-8). the interrupt condition is cleared by reading the rcreg register. the wue bit is automatically cleared once a low-to- high transition is observed on the rx line, following the wake-up event. at this point, the usart module is in idle mode and returns to normal operation. this signals to the user that the sync break event is over. 19.3.4.1 special considerations using auto-wake-up since auto-wake-up functions by sensing rising edge transitions on rx/dt, information with any state changes before the stop bit may signal a false end-of- character and cause data or framing errors. to work properly, therefore, the initial character in the transmission must be all ? 0 ?s. this can be 00h (8 bytes) for standard rs-232 devices, or 000h (12 bits) for lin bus. oscillator start-up time must also be considered, espe- cially in applications using oscillators with longer start- up intervals (i.e., lp, xt or hs/pll mode). the sync break (or wake-up signal) character must be of suffi- cient length, and be followed by a sufficient interval, to allow enough time for the selected oscillator to start and provide proper initialization of the usart. 19.3.4.2 special considerations using the wue bit the timing of wue and rcif events may cause some confusion when it comes to determining the validity of received data. as noted, setting the wue bit places the usart in an idle mode. the wake-up event causes a receive interrupt by setting the rcif bit. the wue bit is cleared after this when a rising edge is seen on rx/ dt. the interrupt condition is then cleared by reading the rcreg register. ordinarily, the data in rcreg will be dummy data and should be discarded. the fact that the wue bit has been cleared (or is still set) and the rcif flag is set should not be used as an indicator of the integrity of the data in rcreg. users should consider implementing a parallel method in firmware to verify received data integrity. to assure that no actual data is lost, check the rcidl bit to verify that a receive operation is not in process. if a receive operation is not occurring, the wue bit may then be set just prior to entering the sleep mode. figure 19-7: auto-wake-up bit (wue) timings during normal operation figure 19-8: auto-wake-up bit (wue) timings during sleep q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 wue bit rx/dt line rcif bit set by user auto-cleared cleared due to user read of rcreg note 1: the usart remains in idle while the wue bit is set. q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 osc1 wue bit rx/dt line rcif bit set by user auto-cleared cleared due to user read of rcreg sleep command executed note 1 note 1: if the wake-up event requires long oscillator warm-up time, the auto-clear of the wue bit can occur while the stposc signal is still active. this sequence should not depend on the presence of q clocks. 2: the usart remains in idle while the wue bit is set. sleep ends
pic18f2331/2431/4331/4431 ds39616b-page 236 preliminary ? 2003 microchip technology inc. 19.3.5 break character sequence the enhanced usart module has the capability of sending the special break character sequences that are required by the lin bus standard. the break char- acter transmit consists of a start bit, followed by 12 ? 0 ? bits and a stop bit. the frame break character is sent whenever the sendb and txen bits (txsta<3> and txsta<5>) are set, while the transmit shift register is loaded with data. note that the value of data written to txreg will be ignored and all ? 0 ?s will be transmitted. the sendb bit is automatically reset by hardware after the corresponding stop bit is sent. this allows the user to preload the transmit fifo with the next transmit byte following the break character (typically, the sync char- acter in the lin specification). note that the data value written to the txreg for the break character is ignored. the write simply serves the purpose of initiating the proper sequence. the trmt bit indicates when the transmit operation is active or idle, just as it does during normal transmis- sion. see figure 19-9 for the timing of the break character sequence. 19.3.5.1 break and sync transmit sequence the following sequence will send a message frame header made up of a break, followed by an auto-baud sync byte. this sequence is typical of a lin bus master. 1. configure the usart for the desired mode. 2. set the txen and sendb bits to setup the break character. 3. load the txreg with a dummy character to initiate transmission (the value is ignored). 4. write ?55h? to txreg to load the sync character into the transmit fifo buffer. 5. after the break has been sent, the sendb bit is reset by hardware. the sync character now transmits in the pre-configured mode. when the txreg becomes empty, as indicated by the txif, the next data byte can be written to txreg. 19.3.6 receiving a break character the enhanced usart module can receive a break character in two ways. the first method forces to configure the baud rate at a frequency of 9/13 the typical speed. this allows for the stop bit transition to be at the correct sampling location (13 bits for break versus start bit and 8 data bits for typ- ical data). the second method uses the auto-wake-up feature described in section 19.3.4 ?auto-wake-up on sync break character? . by enabling this feature, the usart will sample the next two transitions on rx/ dt, cause an rcif interrupt, and receive the next data byte followed by another interrupt. note that following a break character, the user will typically want to enable the auto-baud rate detect feature. for both methods, the user can set the abd bit before placing the usart in its sleep mode. figure 19-9: send break character sequence write to txreg dummy write brg output (shift clock) start bit bit 0 bit 1 bit 11 stop bit break txif bit (transmit buffer reg. empty flag) tx (pin) trmt bit (transmit shift reg. empty flag) sendb (transmit shift reg. empty flag) sendb sampled here auto-cleared
? 2003 microchip technology inc. preliminary ds39616b-page 237 pic18f2331/2431/4331/4431 19.4 usart synchronous master mode the synchronous master mode is entered by setting the csrc bit (txsta<7>). in this mode, the data is transmitted in a half-duplex manner (i.e., transmission and reception do not occur at the same time). when transmitting data, the reception is inhibited and vice versa. synchronous mode is entered by setting bit sync (txsta<4>). in addition, enable bit spen (rcsta<7>) is set in order to configure the rc6/tx/ ck/ss and rc7/rx/dt/sdo i/o pins to ck (clock) and dt (data) lines, respectively. the master mode indicates that the processor trans- mits the master clock on the ck line. clock polarity is selected with the sckp bit (baudctl<5>); setting sckp sets the idle state on ck as high, while clearing the bit, sets the idle state low. this option is provided to support microwire ? devices with this module. 19.4.1 usart synchronous master transmission the usart transmitter block diagram is shown in figure 19-2. the heart of the transmitter is the transmit (serial) shift register (tsr). the shift register obtains its data from the read/write transmit buffer register txreg. the txreg register is loaded with data in software. the tsr register is not loaded until the last bit has been transmitted from the previous load. as soon as the last bit is transmitted, the tsr is loaded with new data from the txreg (if available). once the txreg register transfers the data to the tsr register (occurs in one t cycle ), the txreg is empty and interrupt bit txif (pir1<4>) is set. the interrupt can be enabled/disabled by setting/clearing enable bit txie (pie1<4>). flag bit txif will be set, regardless of the state of enable bit txie, and cannot be cleared in software. it will reset only when new data is loaded into the txreg register. while flag bit txif indicates the status of the txreg register, another bit, trmt (txsta<1>), shows the status of the tsr register. trmt is a read-only bit, which is set when the tsr is empty. no interrupt logic is tied to this bit, so the user must poll this bit in order to determine if the tsr register is empty. the tsr is not mapped in data memory, so it is not available to the user. to set up a synchronous master transmission: 1. initialize the spbrgh:spbrg registers for the appropriate baud rate. set or clear the brgh and brg16 bits, as required, to achieve the desired baud rate. 2. enable the synchronous master serial port by setting bits sync, spen and csrc. 3. if interrupts are desired, set enable bit txie. 4. if 9-bit transmission is desired, set bit tx9. 5. enable the transmission by setting bit txen. 6. if 9-bit transmission is selected, the ninth bit should be loaded in bit tx9d. 7. start transmission by loading data to the txreg register. 8. if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. figure 19-10: synchronous transmission bit 0 bit 1 bit 7 word 1 q1 q2 q3q4 q1 q2 q3 q4 q1 q2 q3 q4 q1q2 q3 q4 q1 q2 q3 q4 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 bit 2 bit 0 bit 1 bit 7 rc7/rx/dt/ rc6/tx/ck/ write to txreg reg txif bit (interrupt flag) txen bit ' 1 ' ' 1 ' word 2 trmt bit write word 1 write word 2 note: sync master mode, spbrg = 0 , continuous transmission of two 8-bit words. sdo pin ss pin rc6/tx/ck/ ss pin (sckp = 0 ) (sckp = 1 )
pic18f2331/2431/4331/4431 ds39616b-page 238 preliminary ? 2003 microchip technology inc. figure 19-11: synchronous transmis sion (through txen) table 19-7: registers associated with synchronous master transmission name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 ? adif rcif txif ? ccp1if tmr2if tmr1if -000 -000 -000 -000 pie1 ? adie rcie txie ? ccp1ie tmr2ie tmr1ie -000 -000 -000 -000 ipr1 ? adip rcip txip ? ccp1ip tmr2ip tmr1ip -000 -000 -000 -000 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x txreg usart transmit register 0000 0000 0000 0000 txsta csrc tx9 txen sync sendb brgh trmt tx9d 0000 0010 0000 0010 baudctl ? rcidl ? sckp brg16 ? wue abden -1-1 0-00 -1-1 0-00 spbrgh baud rate generator register, high byte 0000 0000 0000 0000 spbrg baud rate generator register, low byte 0000 0000 0000 0000 legend: x = unknown, ? = unimplemented, read as ? 0 ?. shaded cells are not used for synchronous master transmission. rc7/rx/dt/sdo pin rc6/tx/ck/ss pin write to txreg reg txif bit trmt bit bit0 bit1 bit2 bit6 bit7 txen bit
? 2003 microchip technology inc. preliminary ds39616b-page 239 pic18f2331/2431/4331/4431 19.4.2 usart synchronous master reception once synchronous mode is selected, reception is enabled by setting either the single receive enable bit, sren (rcsta<5>), or the continuous receive enable bit, cren (rcsta<4>). data is sampled on the rc7/rx/dt/sdo pin on the falling edge of the clock. if enable bit sren is set, only a single word is received. if enable bit cren is set, the reception is continuous until cren is cleared. if both bits are set, then cren takes precedence. to set up a synchronous master reception: 1. initialize the spbrgh:spbrg registers for the appropriate baud rate. set or clear the brgh and brg16 bits, as required, to achieve the desired baud rate. 2. enable the synchronous master serial port by setting bits sync, spen and csrc. 3. ensure bits cren and sren are clear. 4. if interrupts are desired, set enable bit rcie. 5. if 9-bit reception is desired, set bit rx9. 6. if a single reception is required, set bit sren. for continuous reception, set bit cren. 7. interrupt flag bit rcif will be set when reception is complete and an interrupt will be generated if the enable bit rcie was set. 8. read the rcsta register to get the 9th bit (if enabled) and determine if any error occurred during reception. 9. read the 8-bit received data by reading the rcreg register. 10. if any error occurred, clear the error by clearing bit cren. 11. if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. figure 19-12: synchronous reception (master mode, sren) cren bit rc7/rx/dt/sdo rc6/tx/ck/ss write to bit sren sren bit rcif bit (interrupt) read rxreg q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q2 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 q1 q2 q3 q4 ? 0 ? bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7 ? 0 ? q1 q2 q3 q4 note: timing diagram demonstrates sync master mode with bit sren = 1 and bit brgh = 0 . rc6/tx/ck/ss pin pin pin (sckp = 0 ) (sckp = 1 )
pic18f2331/2431/4331/4431 ds39616b-page 240 preliminary ? 2003 microchip technology inc. table 19-8: registers associated with synchronous master reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 ? adif rcif txif ? ccp1if tmr2if tmr1if -000 -000 -000 -000 pie1 ? adie rcie txie ? ccp1ie tmr2ie tmr1ie -000 -000 -000 -000 ipr1 ? adip rcip txip ? ccp1ip tmr2ip tmr1ip -111 -111 -111 -111 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x rcreg usart receive register 0000 0000 0000 0000 txsta csrc tx9 txen sync sendb brgh trmt tx9d 0000 0010 0000 0010 baudctl ? rcidl ? sckp brg16 ? wue abden -1-1 0-00 -1-1 0-00 spbrgh baud rate generator register, high byte 0000 0000 0000 0000 spbrg baud rate generator register, low byte 0000 0000 0000 0000 legend: x = unknown, ? = unimplemented, read as ? 0 ?. shaded cells are not used for synchronous master reception.
? 2003 microchip technology inc. preliminary ds39616b-page 241 pic18f2331/2431/4331/4431 19.5 usart synchronous slave mode synchronous slave mode is entered by clearing bit csrc (txsta<7>). this mode differs from the synchronous master mode in that the shift clock is supplied externally at the rc6/tx/ck/ss pin (instead of being supplied internally in master mode). this allows the device to transfer or receive data while in any low-power mode. 19.5.1 usart synchronous slave transmit the operation of the synchronous master and slave modes are identical, except in the case of the sleep mode. if two words are written to the txreg and then the sleep instruction is executed, the following will occur: a) the first word will immediately transfer to the tsr register and transmit. b) the second word will remain in txreg register. c) flag bit txif will not be set. d) when the first word has been shifted out of tsr, the txreg register will transfer the second word to the tsr and flag bit txif will now be set. e) if enable bit txie is set, the interrupt will wake the chip from sleep. if the global interrupt is enabled, the program will branch to the interrupt vector. to set up a synchronous slave transmission: 1. enable the synchronous slave serial port by setting bits sync and spen and clearing bit csrc. 2. clear bits cren and sren. 3. if interrupts are desired, set enable bit txie. 4. if 9-bit transmission is desired, set bit tx9. 5. enable the transmission by setting enable bit txen. 6. if 9-bit transmission is selected, the ninth bit should be loaded in bit tx9d. 7. start transmission by loading data to the txreg register. 8. if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. table 19-9: registers associated with synchronous slave transmission name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 ? adif rcif txif ? ccp1if tmr2if tmr1if -000 -000 -000 -000 pie1 ? adie rcie txie ? ccp1ie tmr2ie tmr1ie -000 -000 -000 -000 ipr1 ? adip rcip txip ? ccp1ip tmr2ip tmr1ip -000 -000 -000 -000 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x txreg usart transmit register 0000 0000 0000 0000 txsta csrc tx9 txen sync sendb brgh trmt tx9d 0000 0010 0000 0010 baudctl ? rcidl ? sckp brg16 ? wue abden -1-1 0-00 -1-1 0-00 spbrgh baud rate generator register, high byte 0000 0000 0000 0000 spbrg baud rate generator register, low byte 0000 0000 0000 0000 legend: x = unknown, ? = unimplemented, read as ? 0 ?. shaded cells are not used for synchronous slave transmission.
pic18f2331/2431/4331/4431 ds39616b-page 242 preliminary ? 2003 microchip technology inc. 19.5.2 usart synchronous slave reception the operation of the synchronous master and slave modes is identical, except in the case of sleep, or any idle mode and bit sren, which is a ?don't care? in slave mode. if receive is enabled by setting the cren bit prior to entering sleep or any idle mode, then a word may be received while in this low-power mode. once the word is received, the rsr register will transfer the data to the rcreg register; if the rcie enable bit is set, the inter- rupt generated will wake the chip from low-power mode. if the global interrupt is enabled, the program will branch to the interrupt vector. to set up a synchronous slave reception: 1. enable the synchronous master serial port by setting bits sync and spen and clearing bit csrc. 2. if interrupts are desired, set enable bit rcie. 3. if 9-bit reception is desired, set bit rx9. 4. to enable reception, set enable bit cren. 5. flag bit rcif will be set when reception is com- plete. an interrupt will be generated if enable bit rcie was set. 6. read the rcsta register to get the 9th bit (if enabled) and determine if any error occurred during reception. 7. read the 8-bit received data by reading the rcreg register. 8. if any error occurred, clear the error by clearing bit cren. 9. if using interrupts, ensure that the gie and peie bits in the intcon register (intcon<7:6>) are set. table 19-10: registers associated with synchronous slave reception name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/gieh peie/giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 000x 0000 000u pir1 ? adif rcif txif ? ccp1if tmr2if tmr1if 0000 0000 0000 0000 pie1 ? adie rcie txie ? ccp1ie tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 ? adip rcip txip ? ccp1ip tmr2ip tmr1ip -111 -111 -111 -111 rcsta spen rx9 sren cren adden ferr oerr rx9d 0000 -00x 0000 -00x rcreg usart receive register 0000 0000 0000 0000 txsta csrc tx9 txen sync sendb brgh trmt tx9d 0000 0010 0000 0010 baudctl ?rcidl ?sckpbrg16 ? wue abden -1-1 0-00 -1-1 0-00 spbrgh baud rate generator register, high byte 0000 0000 0000 0000 spbrg baud rate generator register, low byte 0000 0000 0000 0000 legend: x = unknown, ? = unimplemented, read as ? 0 ?. shaded cells are not used fo r synchronous slave reception.
? 2003 microchip technology inc. preliminary ds39616b-page 243 pic18f2331/2431/4331/4431 20.0 10-bit high-speed analog- to-digital converter (a/d) module the high-speed analog-to-digital (a/d) converter module allows conversion of an analog signal to a corresponding 10-bit digital number. the a/d module supports up to 5 input channels on pic18f2x31 devices, and up to 9 channels on the pic18f4x31 devices. this high-speed 10-bit a/d module offers the following features:  up to 200k samples per second  two sample and hold inputs for dual-channel simultaneous sampling  selectable simultaneous or sequential sampling modes  4-word data buffer for a/d results  selectable data acquisition timing  selectable a/d event trigger  operation in sleep using internal oscillator these features lend themselves to many applications including motor control, sensor interfacing, data acquisition and process control. in many cases, these features will reduce the software overhead associated with standard a/d modules. the module has 9 registers:  a/d result high register (adresh)  a/d result low register (adresl)  a/d control register 0 (adcon0)  a/d control register 1 (adcon1)  a/d control register 2 (adcon2)  a/d control register 3 (adcon3)  a/d channel select register (adchs)  analog i/o select register 0 (ansel0)  analog i/o select register 1 (ansel1)
pic18f2331/2431/4331/4431 ds39616b-page 244 preliminary ? 2003 microchip technology inc. register 20-1: adcon0: a/d control register 0 u-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 ? ? aconv acsch acmod1 acmod0 go/done adon bit 7 bit 0 bit 7-6 unimplemented: read as ? 0 ? bit 5 aconv : auto-conversion continuous loop or single-shot mode select bit 1 = continuous loop mode enabled 0 = single-shot mode enabled bit 4 acsch : auto-conversion single or multi-channel mode bit 1 = multi-channel mode enabled, single channel mode disabled 0 = single channel mode enabled, multi-channel mode disabled bit 3-2 acmod : auto-conversion mode sequence select bits if acsch = 1 : 00 =sequential mode1 (seqm1). two samples are taken in sequence: 1st sample: group a 2nd sample: group b 01 =sequential mode2 (seqm2). four samples are taken in sequence: 1st sample: group a 2nd sample: group b 3rd sample: group c 4th sample: group d 10 =simultaneous mode1 (stnm1). two samples are taken simultaneously: 1st sample: group a and group b 11 =simultaneous mode2 (stnm2). two samples are taken simultaneously: 1st sample: group a and group b 2nd sample: group c and group d if acsch = 0 , auto-conversion single channel sequence mode enabled : 00 =single ch mode1 (scm1). group a is taken and converted 01 =single ch mode2 (scm2). group b is taken and converted 10 =single ch mode3 (scm3). group c is taken and converted 11 =single ch mode4 (scm4). group d is taken and converted note: group a, b, c, d refer to the adchs register. bit 1 go/done : a/d conversion status bit 1 = a/d conversion cycle in progress. setting this bit starts the a/d conversion cycle. if auto- conversion single-shot mode is enabled (aconv = 0 ), this bit is automatically cleared by hardware when the a/d conversion (single or multi-channel depending on acmod settings) has completed. if auto-conversion continuous loop mode is enabled (aconv = 1 ), this bit remains set after the user/trigger has set it (continuous conversions). it may be cleared manually by the user to stop the conversions. 0 = a/d conversion or multiple conversions completed/not in progress bit 0 adon: a/d on bit 1 = a/d converter module is enabled (after brief power-up delay, starts continuous sampling) 0 = a/d converter module is disabled legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at reset ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 245 pic18f2331/2431/4331/4431 register 20-2: adcon1: a/d control register 1 r/w-0 r/w-0 u-0 r/w-0 r-0 r-0 r-0 r-0 vcfg1 vcfg0 ? fifoen bfemt bfovfl adpnt1 adpnt0 bit 7 bit 0 bit 7-6 vcfg<1:0>: a/d v ref + and a/d v ref - source selection bits 00 =v ref + = av dd , v ref - = av ss , (an2 and an3 are analog inputs or digital i/o) 01 =v ref + = external v ref +, v ref - = av ss , (an2 is an analog input or digital i/o) 10 =v ref + = av dd , v ref - = external v ref -, (an3 is an analog input or digital i/o) 11 =v ref + = external v ref -, v ref - = external v ref - bit 5 unimplemented : read as ? 0 ? bit 4 fifoen : fifo buffer enable bit 1 = fifo is enabled 0 = fifo is disabled bit 3 bfemt : buffer empty bit 1 = fifo is empty 0 = fifo is not empty (at least one of four locations has unread a/d result data) bit 2 bfovfl : buffer overflow bit 1 = a/d result has overwritten a buffer location that has unread data 0 = a/d result has not overflowed bit 1-0 adpnt<1:0> : buffer read pointer locations bits designates the location to be read next. 00 = buffer address 0 01 = buffer address 1 10 = buffer address 2 11 = buffer address 3 legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at reset ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 246 preliminary ? 2003 microchip technology inc. register 20-3: adcon2 ? a/d contro l register 2 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 adfm acqt3 acqt2 acqt1 acqt0 adcs2 adcs1 adcs0 bit 7 bit 0 bit 7 adfm: a/d result format select bit 1 = right justified 0 = left justified bit 6-3 acqt<3:0> : a/d acquisition time select bits 0000 = no delay(1) (conversion starts immediately when go/done is set) 0001 =2 t ad 0010 =4 t ad 0011 =6 t ad 0100 =8 t ad 0101 =10 t ad 0110 =12 t ad 0111 =16 t ad 1000 =20 t ad 1001 =24 t ad 1010 =28 t ad 1011 =32 t ad 1100 =36 t ad 1101 =40 t ad 1110 =48 t ad 1111 =64 t ad bit 2-0 adcs<2:0>: a/d conversion clock select bits 000 =f osc /2 001 =f osc /8 010 =f osc /32 011 =f rc /4 (2) 100 =f osc /4 101 =f osc /16 110 =f osc /64 111 =f rc (internal a/d rc oscillator) note 1: if the a/d clock source is selected as rc, a time of t cy is added before sampling/ conversion starts. 2: due to an increased frequency of the internal a/d rc oscillator, f rc /4 provides clock frequencies compatible with previous a/d modules. 3: in sequential mode t acq should be 12 t ad or greater. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at reset ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 247 pic18f2331/2431/4331/4431 register 20-4: adcon3: a/d control register 3 r/w-0 r/w-0 u-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 adrs1 adrs0 ? ssrc4 ssrc3 ssrc2 ssrc1 ssrc0 bit 7 bit 0 bit 7-6 adrs<1:0> : a/d result buffer depth interrupt select control bits for continuous loop mode the adrs bits are ignored in single-shot mode. 00 =interrupt is generated when each word is written to the buffer 01 =interrupt is generated when the 2nd & 4th words are written to the buffer 10 =interrupt is generated when the 4th word is written to the buffer 11 =unimplemented bit 5 unimplemented: read as ? 0 ? bit 4:0 ssrcx<4:0>: a/d trigger source select bits 00000 =all triggers disabled xxxx1 =external interrupt rc3/int0 starts a/d sequence xxx1x =timer5 starts a/d sequence xx1xx =input capture 1 (ic1) starts a/d sequence x1xxx =ccp2 compare match starts a/d sequence 1xxxx =power control pwm module rising edge starts a/d sequence note 1: ssrcx<4:0> bits can be set such that any of the triggers will start conversion (e.g. ssrcx<4:0)> = 00101 , will trigger the a/d conversion sequence when rc3/int0 or input capture 1 event occurs). legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at reset ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 248 preliminary ? 2003 microchip technology inc. register 20-5: adchs: a/d channel select register r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 r/w-0 gdsel1 gdsel0 gbsel1 gbsel0 gcsel1 gcsel0 gasel1 gasel0 bit 7 bit 0 bit 7-6 gdsel1:gdsel0 : group d select bits s/h-2 positive input 00 =an3 01 =an7 (1) 1x =reserved bit 5-4 gbsel1:gbsel0 : group b select bits s/h-2 positive input 00 =an1 01 =an5 (1) 1x =reserved bit 3-2 gcsel1:gcsel0 : group c select bits s/h-1 positive input 00 =an2 01 =an6 (1) 1x =reserved bit 1-0 gasel1:gasel0 : group a select bits s/h-1 positive input 00 =an0 01 =an4 10 =an8 (1) 11 =reserved note 1: an5 through an8 are available only in pic18f4x31 devices. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at reset ?1? = bit is set ?0? = bit is cleared x = bit is unknown
? 2003 microchip technology inc. preliminary ds39616b-page 249 pic18f2331/2431/4331/4431 register 20-6: ansel0: analog select register 0 (1) register 20-7: ansel1: analog select register 1 (1) r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 r/w-1 ans7 (2) ans6 (2) ans5 (2) ans4 ans3 ans2 ans1 ans0 bit 7 bit 0 bit 7-0 ans<7:0>: analog input function select bits correspond to pins an<7:0> 1 = analog input 0 = digital i/o note 1: setting a pin to an analog input disables the digital input buffer. the corresponding tris bit should be set for an input and cleared for an output (analog or digital). the ansx bits directly correspond to the anx pi ns (e.g., ans0 = an0, ans1 = an1, etc.) unused ansx bits are to be read as ? 0 ?. 2: ans7 through ans5 are available only on pic18f4x31 devices. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at reset ?1? = bit is set ?0? = bit is cleared x = bit is unknown u-0 u-0 u-0 u-0 u-0 u-0 u-0 r/w-1 ? ? ? ? ? ? ?ans8 (2) bit 15 bit 8 bit 15-9 unimplemented: read as ? 0 ? bit 8 ans8: analog input function select bit 1 = analog input 0 = digital i/o note 1: setting a pin to an analog input disables the digital input buffer. the corresponding tris bit should be set for an input and cleared for an output (analog or digital). the ansx bits directly correspond to the anx pi ns (e.g., ans8 = an8, ans9 = an9, etc.) unused ansx bits are to be read as ? 0 ?. 2: ans8 is available only on pic18f4x31 devices. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? -n = value at reset ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 250 preliminary ? 2003 microchip technology inc. the a/d channels are grouped into four sets of 2 or 3 channels. for the pic18f2x31 devices, an0 and an4 are in group a, an1 is in group b, an2 is in group c and an3 is in group d. for the pic18f4x31 devices, an0, an4 and an8 are in group a, an1 and an5 are in group b, an2 and an6 are in group c and an3 and an7 are in group d. the selected channel in each group is selected by configuring the a/d channel select register, adchs. the analog voltage reference is software selectable to either the device?s positive and negative analog supply voltage (av dd and av ss ), or the voltage level on the ra3/an3/v ref +/cap2/qea and ra2/an2/v ref -/ cap1/indx, or some combination of supply and external sources. register adcon1 controls the voltage reference settings. the a/d converter has a unique feature of being able to operate while the device is in sleep mode. to operate in sleep, the a/d conversion clock must be derived from the a/d?s internal rc oscillator. a device reset forces all registers to their reset state. this forces the a/d module to be turned off and any conversion in progress is aborted. each port pin associated with the a/d converter can individually be configured as an analog input or digital i/o using the ansel0 and ansel1 registers. the adresh and adresl registers contain the value in the result buffer pointed to by adpnt<1:0> (adcon1<1:0>). the result buffer is a 4-deep circular buffer that has an empty status bit, bemt (adcon1<3>), and an overflow status bit, bovfl (adcon1<2>). figure 20-1: a/d block diagram s/h + - adresh, adresl v ref + av ss av dd adc an0 an4 s/h-1 s/h-2 v ref - s/h + - analog mux analog mux an8 (1) an2/v ref - an6 (1) seq. cntrl. 1 2 3 4 mux 4x10-bit fifo vcfg<1:0> vrefl vrefh acmod, gxsel<1:0> 10 01 11 av ss av ss 00 10 adpnt<1:0> acmod, gxsel<1:0> an1 an5 (1) an3/v ref + an7 (1) aconv acscm acmod note 1: an5 through an8 are available only on pic18f4x31 devices.
? 2003 microchip technology inc. preliminary ds39616b-page 251 pic18f2331/2431/4331/4431 20.1 configuring the a/d converter the a/d converter has two types of conversion, two modes of operation and eight different sequencing modes. these features are controlled by the aconv bit (adcon0<5>), acsh bit (adcon0<4>) and acmod<1:0> bits (adcon0<3:2>). in addition, the a/d channels are divided into four groups as defined in the adchs register. table 20-1 shows the sequence configurations as controlled by acsch and acmod<1:0>. table 20-1: auto-conversion sequence configurations 20.1.1 conversion type two types of conversions exist in the high-speed 10-bit a/d converter module that are selected using the aconv bit. single-shot mode allows a single conversion or sequence to be when aconv = ? 0 ?. at the end of the sequence, the go/done bit will be automatically cleared and the interrupt flag, adif, will be set. when using single-shot mode and configured for simultaneous mode, stnm2, acquisition time must be used to ensure proper conversion of the analog input signals. continuous loop mode allows the defined sequence to be executed in a continuous loop when aconv = ? 1 ?. in this mode, either the user can trigger the start of con- version by setting the go/done bit or one of the a/d triggers can start the conversion. the interrupt flag adif is set based on the configuration of the bits adrs<1:0> (adcon3<7:6>). in simultaneous modes, stnm1 and stnm2, acquisition time must be config- ured to ensure proper conversion of the analog input signals. 20.1.2 conversion mode the acsch bit (adcon0<4>) controls how many channels are used in the configured sequence. when clear, the a/d is configured for single channel conver- sion and will convert the group selected by acmod<1:0> and channel selected by gxsel<1:0> (adchs). when acsch = ? 1 ?, the a/d is configured for multiple channel conversion and the sequence is defined by acmod<1:0>. mode acsch acmod description multi-channel sequential mode1 (seqm1) 100 group a and b are sampled and converted sequentially multi-channel sequential mode2 (seqm2) 101 group a, b, c and d are sampled and converted sequentially multi-channel simultaneous mode1 (stnm1) 110 group a and b are sampled simultaneously and converted sequentially multi-channel simultaneous mode2 (stnm2) 111 group a and b are sampled simultaneously, then converted sequentially. then, group c and d are sampled simultaneously, then converted sequentially. single channel mode1 (scm1) 000 group a is sampled and converted single channel mode2 (scm2) 001 group b is sampled and converted single channel mode3 (scm3) 010 group c is sampled and converted single channel mode4 (scm4) 011 group d is sampled and converted
pic18f2331/2431/4331/4431 ds39616b-page 252 preliminary ? 2003 microchip technology inc. 20.1.3 conversion sequencing the acmod<1:0> bits control the sequencing of the a/d conversions. when acsch = 0 , the a/d is configured to sample and convert a single channel. the acmod bits select which group to perform the conversions and the gxsel<1:0> bits select which channel in the group is to be converted. if single-shot mode is enabled, the a/d interrupt flag will be set after the channel is converted. if continuous loop mode is enabled, the a/d interrupt flag will be set according to the adrs<1:0> bits. when acshc = 1 , multiple channel sequencing is enabled and two sub-modes can be selected. the first mode is sequential mode with two settings. the first setting is called seqm1 and first samples and converts the selected group a channel and then samples and converts the selected group b channel. the second mode is called seqm2, and it samples and converts a group a channel, group b channel, group c channel and finally a group d channel. the second multiple channel sequencing sub-mode is simultaneous sampling mode. in this mode, there are also two settings. the first setting is called stnm1 and uses the two sample and hold circuits on the a/d module. the selected group a and b channels are simultaneously sampled and then the group a channel is converted followed by the conversion of the group b channel. the second setting is called stnm2 and starts the same as stnm1 but follows it with a simultaneous sample of group c and d channels. the a/d module will then convert the group c channel followed by the group d channel. 20.1.4 triggering a/d conversions the pic18f2331/2431/4331/4431 devices are capable of triggering conversions from many different sources. the same method used by all other microcontrollers of setting the go/done bit still works. the other trigger sources are:  rc3/int0 pin  timer5 overflow  input capture 1 (ic1)  ccp2 compare match  power control pwm rising edge these triggers are enabled using the ssrc<4:0> bits (adcon3<4:0>). any combination of the five sources can trigger a conversion by simply setting the corre- sponding bit in adcon3. when the trigger occurs, the go/done bit is automatically set by the hardware and then cleared once the conversion completes. 20.1.5 a/d module initialization steps the following steps should be followed to initialize the a/d module: 1. configure the a/d module: a) configure analog pins, voltage reference and digital i/o b) select a/d input channels c) select a/d auto-conversion mode (single-shot or continuous loop) d) select a/d conversion clock e) select a/d conversion trigger 2. configure a/d interrupt (if required): a) set gie bit b) set peie bit c) set adie bit d) clear adif bit e) select a/d trigger setting f) select a/d interrupt priority 3. turn on adc: a) set adon bit in adcon0 register b) wait the required power-up setup time, about 5-10 s 4. start sample/conversion sequence: a) sample for a minimum of 2t ad and start conversion by setting the go/done bit. the go/done bit is set by the user in software or by the module if initiated by a trigger. b) if t acq is assigned a value (multiple of t ad ), then setting the go/done bit starts a sample period of the t acq value, then starts a conversion. 5. wait for a/d conversion/conversions to complete using one of the following options: a) poll for the go/done bit to be cleared if in single-shot mode. b) wait for the a/d interrupt flag (adif) to be set. c) poll for the bfemt bit to be cleared to signify that at least the first conversion has completed. 6. read a/d results, clear adif flag, reconfigure trigger.
? 2003 microchip technology inc. preliminary ds39616b-page 253 pic18f2331/2431/4331/4431 20.2 a/d result buffer the a/d module has a 4-level result buffer with an address range of 0 to 3, enabled by setting the fifoen bit in the adcon1 register. this buffer is implemented in a circular fashion where the a/d result is stored in one location and the address is incremented. if the address is greater than 3, the pointer is wrapped back around to 0. the result buffer has a buffer empty flag, bemt, indicating when any data is in the buffer. it also has an overflow flag, bovfl, which indicates when a new sample has overwritten a location that was not previously read. associated with the buffer is a pointer to the address for the next read operation. the adpnt<1:0> bits configure the address for the next read operation. these bits are read-only. the result buffer also has a configurable interrupt trigger level that is configured by the adrs<1:0> bits. the user has three selections: interrupt flag set on every write to the buffer, interrupt on every second write to the buffer, or interrupt on every fourth write to the buffer. adpnt<1:0> is reset to ? 00 ? every time a conversion sequence is started (either by setting the go/done bit, or on a trigger). 20.3 a/d acquisition requirements for the a/d converter to meet its specified accuracy, the charge holding capacitor (c hold ) must be allowed to fully charge to the input channel voltage level. the analog input model is shown in figure 20-2. the source impedance (r s ) and the internal sampling switch (r ss ) impedance directly affect the time required to charge the capacitor c hold . the sampling switch (r ss ) impedance varies over the device voltage (v dd ). the source impedance affects the offset voltage at the analog input (due to pin leakage current). the maximum recommended impedance for analog sources is 2.5 k ? . after the analog input channel is selected (changed), the channel must be sampled for at least the minimum acquisition time before starting a conversion. to calculate the minimum acquisition time, equation 20-1 may be used. this equation assumes that 1/2 lsb error is used (1024 steps for the a/d). the 1/2 lsb error is the maximum error allowed for the a/d to meet its specified resolution. example 20-1 shows the calculation of the minimum required acquisition time t acq . in this case, the converter module is fully powered up at the outset and therefore the amplifier settling time, t amp , is negligible. this calculation is based on the following application system assumptions: c hold =9 pf rs = 100 ? conversion error 1/2 lsb v dd =5v rss = 6 k ? temperature = 50 c (system max.) v hold = 0v @ time = 0 equation 20-1: acquisition time equation 20-2: minimum a/d holding capacitor charging time note: when right justified, reading adresl increments adpnt. when left justified, reading adresh increments adpnt. note: when the conversion is started, the holding capacitor is disconnected from the input pin. t acq = amplifier settling time + holding capacitor charging time + temperature coefficient =t amp + t c + t coff v hold = (v ref ? (v ref /2048))  (1 ? e (-tc/c hold (r ic + r ss + r s )) ) or t c = -(c hold )(r ic + r ss + r s ) ln(1/2048)
pic18f2331/2431/4331/4431 ds39616b-page 254 preliminary ? 2003 microchip technology inc. example 20-1: calculating the minimum required acquisition time figure 20-2: analog input model t acq =t amp + t c + t coff t amp = negligible t coff =(temp ? 25 c)(0.005 s/ c) (50 c ? 25 c)(0.005 s/ c) = .13 s temperature coefficient is only required for temperatures > 25 c. below 25 c, t coff = 0 s. t c ? -(c hold ) (r ic + r ss + r s ) ln(1/2047) s -(9 pf) (1 k ? + 6 k ? + 100 ? ) ln(0.0004883) s = .49 s + .13 s = .62 s t acq = 0 + .62 s + .13 s = .75 s note: if the converter module has been in sleep mode, t amp is 2.0 s from the time the part exits sleep mode. v ain c pin rs anx 5 pf v dd v t = 0.6v v t = 0.6v i leakage r ic 1k sampling switch ss r ss c hold = 9 pf v ss 6v sampling switch 5v 4v 3v 2v 567891011 (k ? ) v dd 500 na legend: c pin v t i leakage r ic ss c hold = input capacitance = threshold voltage = leakage current at the pin due to = interconnect resistance = sampling switch = sample/hold capacitance (from dac) various junctions = sampling switch resistance r ss note: for v dd < 2.7v and temperatures below 0oc, v ain should be restricted to range: v ain < v dd /2.
? 2003 microchip technology inc. preliminary ds39616b-page 255 pic18f2331/2431/4331/4431 20.4 a/d voltage references if external voltage references are used instead of the internal av dd and av ss sources, the source impedance of the v ref + and v ref - voltage sources must be considered. during acquisition, currents supplied by these sources are insignificant. however, during conversion, the a/d module sinks and sources current through the reference sources. in order to maintain the a/d accuracy, the voltage reference source impedances should be kept low to reduce voltage changes. these voltage changes occur as reference currents flow through the reference source impedance. 20.5 selecting and configuring automatic acquisition time the adcon2 register allows the user to select an acquisition time that occurs each time an a/d conver- sion is triggered. when the go/done bit is set, sampling is stopped and a conversion begins. the user is responsible for ensuring the required acquisition time has passed between selecting the desired input channel and the start of conversion. this occurs when the acqt3:acqt0 bits (adcon2<6:3>) remain in their reset state (? 0000 ?). if desired, the acqt bits can be set to select a programmable acquisition time for the a/d module. when triggered, the a/d module continues to sample the input for the selected acquisition time, then automatically begins a conversion. since the acquisition time is programmed, there may be no need to wait for an acquisition time between selecting a channel and triggering the a/d. if an acquisition time is programmed, there is nothing to indicate if the acquisition time has ended, or if the conversion has begun. 20.6 selecting the a/d conversion clock the a/d conversion time per bit is defined as t ad . the a/d conversion requires 12 t ad per 10-bit conversion. the source of the a/d conversion clock is software selectable. there are eight possible options for t ad : 2 t osc 4 t osc 8 t osc 16 t osc 32 t osc 64 t osc  internal rc oscillator  internal rc oscillator/4 for correct a/d conversions, the a/d conversion clock (t ad ) must be as short as possible, but greater than the minimum t ad (approximately 416 s, see parameter 130 for more information). table 20-2 shows the resultant t ad times derived from the device operating frequencies and the a/d clock source selected. note: when using external references, the source impedance of the external voltage references must be less than 75 ? in order to achieve the specified adc resolution. a higher reference source impedance will increase the adc offset and gain errors. resistive voltage dividers will not provide a low enough source impedance. to ensure the best possible adc perfor- mance, external vref inputs should be buffered with an op-amp or other low impedance circuit.
pic18f2331/2431/4331/4431 ds39616b-page 256 preliminary ? 2003 microchip technology inc. table 20-2: t ad vs. device operating frequencies 20.7 operation in power-managed modes the selection of the automatic acquisition time and a/d conversion clock is determined in part by the clock source and frequency while in a power-man- aged mode. if the a/d is expected to operate while the device is in a power-managed mode, the acqt3:acqt0 and adcs2:adcs0 bits in adcon2 should be updated in accordance with the power-managed mode clock that will be used. after the power-managed mode is entered (either of the power-managed run modes), an a/d acquisition or conversion may be started. once an acquisition or conversion is started, the device should continue to be clocked by the same power-managed mode clock source until the conversion has been com- pleted. if desired, the device may be placed into the corresponding power-managed idle mode during the conversion. if the power-managed mode clock frequency is less than 1 mhz, the a/d rc clock source should be selected. operation in sleep mode requires the a/d rc clock to be selected. if bits acqt3:acqt0 are set to ? 0000 ?, and a conversion is started, the conversion will be delayed one instruction cycle to allow execution of the sleep instruction and entry to sleep mode. the idlen and scs bits in the osccon register must have already been cleared prior to starting the conversion. 20.8 configuring analog port pins the ansel0, ansel1, trisa and trise registers all configure the a/d port pins. the port pins needed as analog inputs must have their corresponding tris bits set (input). if the tris bit is cleared (output), the digital output level (v oh or v ol ) will be converted. the a/d operation is independent of the state of the ansel0, ansel1 and the tris bits. ad clock source (t ad ) maximum device frequency operation adcs2:adcs0 pic18fxx31 pic18lfxx31 (4) 2 t osc 000 4.8 mhz 666 khz 4 t osc 100 9.6 mhz 1.33 mhz 8 t osc 001 19.2 mhz 2.66 mhz 16 t osc 101 38.4 mhz 5.33 mhz 32 t osc 010 40.0 mhz 10.65 mhz 64 t osc 110 40.0 mhz 21.33 mhz rc/4 (3) 011 1.00 mhz (1) 1.00 mhz (2) rc (3) 111 4.0 mhz (2) 4.0 mhz (2) note 1: the rc source has a typical t ad time of 2-6 s. 2: the rc source has a typical t ad time of 0.5-1.5 s. 3: for device frequencies above 1 mhz, the device must be in sleep for the entire conversion or the a/d accuracy may be out of specification, unless in single-shot mode. 4: low-power devices only. note: the a/d can operate in sleep mode only when configured for single-shot opera- tion. if the part is in sleep mode, and it is possible for a source other than the a/d module to wake the part, the user must poll adcon to ensure it is clear before reading the result. note 1: when reading the port register, all pins configured as analog input channels will read as cleared (a low level). pins configured as digital inputs will convert an analog input. analog levels on a digitally configured input will be accurately converted. 2: analog levels on any pin defined as a digital input may cause the digital input buffer to consume current out of the device?s specification limits.
? 2003 microchip technology inc. preliminary ds39616b-page 257 pic18f2331/2431/4331/4431 20.9 a/d conversions figure 20-3 shows the operation of the a/d converter after the go bit has been set and the acqt2:acqt0 bits are cleared. a conversion is started after the follow- ing instruction to allow entry into sleep mode before the conversion begins. the internal a/d rc oscillator must be selected to perform a conversion in sleep. figure 20-4 shows the operation of the a/d converter after the go bit has been set and the acqt3:acqt0 bits are set to ? 010 ?, and selecting a 4 t ad acquisition time before the conversion starts. clearing the go/done bit during a conversion will abort the current conversion. the resulting buffer loca- tion will contain the partially completed a/d conversion sample. this will not set the adif flag, therefore, the user must read the buffer location before a conversion sequence overwrites it. after the a/d conversion is completed or aborted, a 2t ad wait is required before the next acquisition can be started. after this wait, acquisition on the selected channel is automatically started. figure 20-3: a/d conversion t ad cycles (a cqt <2:0> = 000 , t acq = 0 ) figure 20-4: a/d conversion t ad cycles (a cqt <3:0> = 0010 , t acq = 4 t ad ) note: the go/done bit should not be set in the same instruction that turns on the a/d. t ad 1 t ad 2 t ad 3 t ad 4 t ad 5 t ad 6 t ad 7 t ad 8 t ad 11 t ad 9 t ad 10 go bit cleared on the rising edge of q1 after the first q3 conversion starts b2 b8 b7 b6 b5 b4 b3 b9 b1 b0 following t ad 11 (1) , and result buffer is loaded. go bit is set, and holding cap is disconnected from analog input note 1: conversion time is a minimum of 11 t ad + 2 t cy , and a maximum of 11 t ad + 6 t cy . a/d triggered (holding capacitor is disconnected) 1 2 3 4 t acqt cycles automatic acquisition time t ad 1 t ad 2 t ad 3 t ad 4 t ad 5 t ad 6 t ad 7 t ad 8 t ad 11 t ad 9 t ad 10 go bit cleared on the rising edge of q1 after the first q3 conversion starts b2 b8 b7 b6 b5 b4 b3 b9 b1 b0 following t ad 11 (1) and result buffer is loaded. t ad cycles note 1: in continuous modes, next conversion starts at the end of t ad 12.
pic18f2331/2431/4331/4431 ds39616b-page 258 preliminary ? 2003 microchip technology inc. 20.9.1 a/d result register the adresh:adresl register pair is the location where the 10-bit a/d result is loaded at the completion of the a/d conversion. this register pair is 16-bits wide. the a/d module gives the flexibility to left- or right- justify the 10-bit result in the 16-bit result register. the a/d format select bit (adfm) controls this justification. figure 20-5 shows the operation of the a/d result justification. the extra bits are loaded with ? 0 ?s. when an a/d result will not overwrite these locations (a/d disable), these registers may be used as two general purpose 8-bit registers. figure 20-5: a/d result justification equation 20-3: conversion time for multichannel modes 10-bit result adresh adresl 0000 00 adfm = 0 0 2 1 0 7 7 10-bit result adresh adresl 10-bit result 0000 00 7 0 7 6 5 0 adfm = 1 right justified left justified sequential mode: simultaneous mode: t = (t acq ) a + (t con ) a + [(t acq ) b - 12t ad ] + (t con ) b + [(t acq ) c - 12t ad ] + (t con ) c + [(t acq ) d - 12t ad ] + (t con ) d t = t acq + (t con ) a + (t con ) b + t acq + (t con ) c + (t con ) d
? 2003 microchip technology inc. preliminary ds39616b-page 259 pic18f2331/2431/4331/4431 table 20-3: summary of a/d registers name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 value on por, bor value on all other resets intcon gie/ gieh peie/ giel tmr0ie int0ie rbie tmr0if int0if rbif 0000 0000 0000 0000 pir1 pspif adif rcif txif sspif ccp1if tmr2if tmr1if 0000 0000 0000 0000 pie1 pspie adie rcie txie sspie ccp1ie tmr2ie tmr1ie 0000 0000 0000 0000 ipr1 pspip adip rcip txip sspip ccp1ip tmr2ip tmr1ip 1111 1111 1111 1111 pir2 oscfif cmif ? eeif bclif lvdif tmr3if ccp2if 00-0 0000 00-0 0000 pie2 oscfie cmie ? eeie bclie lvdie tmr3ie ccp2ie 00-0 0000 00-0 0000 ipr2 oscfip cmip ? eeip bclip lvdip tmr3ip ccp2ip 11-1 1111 11-1 1111 adresh a/d result register high byte xxxx xxxx uuuu uuuu adresl a/d result register low byte xxxx xxxx uuuu uuuu adcon0 ? ? aconv acmod1 acmod0 chs0 go/done adon 00-1 0000 00-1 0000 adcon1 vcfg1 vcfg0 ? fifoen bfemt bfovfl adpnt1 adpnt0 --00 qqqq --00 qqqq adcon2 adfm acqt3 acqt2 acqt1 acqt0 adcs2 adcs1 adcs0 0-00 0000 0-00 0000 adcon3 adrs1 adrs0 ? ssrc4 ssrc3 ssrc2 ssrc1 ssrc0 00-0 0000 00-0 0000 adchs gdsel1 gdsel0 gbsel1 gbsel0 gcsel1 gcsel0 gasel1 gasel0 0000 0000 0000 0000 ansel0 ans7 (6) ans6 (6) ans5 (6) ans4 ans3 ans2 ans1 ans0 1111 1111 1111 1111 ansel1 ? ? ? ? ? ? ?ans8 (5) ---- ---1 ---- ---1 porta ra7 (4) ra6 (4) ra5 ra4 ra3 ra2 ra1 ra0 --0x 0000 --0u 0000 trisa trisa7 (4) trisa6 (4) data direction control register for porta --11 1111 --11 1111 porte (2) ? ? ? ? re3 (1) read porte pins, write late (4) ---- xxxx ---- uuuu trise (3) ibf obe ibov pspmode ? porte data direction 0000 -111 0000 -111 late (3) ? ? ? ? porte output data latch ---- -xxx ---- -uuu legend: x = unknown, u = unchanged, ? = unimplemented, read as ? 0 ?, q = value depends on condition. shaded cells are not used for a/d conversion. note 1: re3 port bit is available only as an input pin w hen mclre bit in configuration register is ? 0 ?. 2: this register is not impl emented on pic18f2x31 devices. 3: these bits are not implemented on pic18f2x31 devices. 4: these pins may be configured as port pi ns depending on the oscillator mode selected. 5: ans5 through ans8 are availabl e only on the pic18f4x31 devices. 6: not available on 28-pin devices.
pic18f2331/2431/4331/4431 ds39616b-page 260 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 261 pic18f2331/2431/4331/4431 21.0 low-voltage detect in many applications, the ability to determine if the device voltage (v dd ) is below a specified voltage level is a desirable feature. a window of operation for the application can be created, where the application software can do ?housekeeping tasks? before the device voltage exits the valid operating range. this can be done using the low-voltage detect module (lvd). this module is a software programmable circuitry, where a device voltage trip point can be specified. when the voltage of the device becomes lower then the specified point, an interrupt flag is set. if the interrupt is enabled, the program execution will branch to the interrupt vector address and the software can then respond to that interrupt source. the low-voltage detect circuitry is completely under software control. this allows the circuitry to be turned off by the software, which minimizes the current consumption for the device. figure 21-1 shows a possible application voltage curve (typically for batteries). over time, the device voltage decreases. when the device voltage equals voltage v a , the lvd logic generates an interrupt. this occurs at time t a . the application software then has the time, until the device voltage is no longer in valid operating range, to shut down the system. voltage point v b is the minimum valid operating voltage specification. this occurs at time t b . the difference t b ? t a is the total time for shutdown. the block diagram for the lvd module is shown in figure 21-2. a comparator uses an internally gener- ated reference voltage as the set point. when the selected tap output of the device voltage crosses the set point (is lower than), the lvdif bit is set. each node in the resistor divider represents a ?trip point? voltage. the ?trip point? voltage is the minimum supply voltage level at which the device can operate before the lvd module asserts an interrupt. when the supply voltage is equal to the trip point, the voltage tapped off of the resistor array is equal to the 1.2v internal reference voltage generated by the voltage reference module. the comparator then generates an interrupt signal setting the lvdif bit. this voltage is software programmable to any one of 16 values (see figure 21-2). the trip point is selected by programming the lvdl3:lvdl0 bits (lvdcon<3:0>). figure 21-1: typical low-voltage detect application time voltage v a v b t a t b v a = lvd trip point v b = minimum valid device operating voltage legend :
pic18f2331/2431/4331/4431 ds39616b-page 262 preliminary ? 2003 microchip technology inc. figure 21-2: low-voltage detect (lvd) block diagram the lvd module has an additional feature that allows the user to supply the sense voltage to the module from an external source. this mode is enabled when bits lvdl3:lvdl0 are set to ? 1111 ?. in this state, the comparator input is multiplexed from the external input pin, lvdin (figure 21-3). this gives users flexibility, because it allows them to configure the low-voltage detect interrupt to occur at any voltage in the valid operating range. figure 21-3: low-voltage detect (lvd) with external input block diagram lvdif v dd 16 to 1 mux lvden lvd control register internally generated reference voltage lvdin 1.2v lvd en lvd control 16 to 1 mux bgap boden lvden vxen lvdin register v dd v dd externally generated trip point
? 2003 microchip technology inc. preliminary ds39616b-page 263 pic18f2331/2431/4331/4431 21.1 control register the low-voltage detect control register controls the operation of the low-voltage detect circuitry. register 21-1: lvdcon register u-0 u-0 r-0 r/w-0 r/w-0 r/w-1 r/w-0 r/w-1 ? ? irvst lvden lvdl3 lvdl2 lvdl1 lvdl0 bit 7 bit 0 bit 7-6 unimplemented: read as ? 0 ? bit 5 irvst: internal reference voltage stable flag bit 1 = indicates that the low-voltage detect logic will generate the interrupt flag at the specified voltage range 0 = indicates that the low-voltage detect logic will not generate the interrupt flag at the specified voltage range and the lvd interrupt should not be enabled bit 4 lvden: low-voltage detect power enable bit 1 = enables lvd, powers up lvd circuit 0 = disables lvd, powers down lvd circuit bit 3-0 lvdl3:lvdl0: low-voltage detection limit bits 1111 = external analog input is used (input comes from the lvdin pin) 1110 = 4.23v - 4.96v 1101 = 3.93v - 4.62v 1100 = 3.75v - 4.40v 1011 = 3.56v - 4.18v 1010 = 3.38v - 3.96v 1001 = 3.29v - 3.86v 1000 = 3.09v - 3.63v 0111 = 2.82v - 3.31v 0110 = 2.64v - 3.10v 0101 = 2.55v - 2.99v 0100 = 2.35v - 2.76v 0011 = 2.26v - 2.65v 0010 = 2.08v - 2.44v 0001 = reserved 0000 = reserved note: lvdl3:lvdl0 modes which result in a trip point below the valid operating voltage of the device are not tested. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por ?1? = bit is set ?0? = bit is cleared x = bit is unknown
pic18f2331/2431/4331/4431 ds39616b-page 264 preliminary ? 2003 microchip technology inc. 21.2 operation depending on the power source for the device voltage, the voltage normally decreases relatively slowly. this means that the lvd module does not need to be constantly operating. to decrease the current requirements, the lvd circuitry only needs to be enabled for short periods, where the voltage is checked. after doing the check, the lvd module may be disabled. each time that the lvd module is enabled, the circuitry requires some time to stabilize. after the circuitry has stabilized, all status flags may be cleared. the module will then indicate the proper state of the system. the following steps are needed to set up the lvd module: 1. write the value to the lvdl3:lvdl0 bits (lvdcon register), which selects the desired lvd trip point. 2. ensure that lvd interrupts are disabled (the lvdie bit is cleared or the gie bit is cleared). 3. enable the lvd module (set the lvden bit in the lvdcon register). 4. wait for the lvd module to stabilize (the irvst bit to become set). 5. clear the lvd interrupt flag, which may have falsely become set until the lvd module has stabilized (clear the lvdif bit). 6. enable the lvd interrupt (set the lvdie and the gie bits). figure 21-4 shows typical waveforms that the lvd module may be used to detect. figure 21-4: low-voltage detect waveforms v lvd v dd lvdif v lvd v dd enable lvd internally generated t ivrst lvdif may not be set enable lvd lvdif lvdif cleared in software lvdif cleared in software lvdif cleared in software, case 1: case 2: lvdif remains set since lvd condition still exists reference stable internally generated reference stable t ivrst
? 2003 microchip technology inc. preliminary ds39616b-page 265 pic18f2331/2431/4331/4431 21.2.1 reference voltage set point the internal reference voltage of the lvd module may be used by other internal circuitry (the programmable brown-out reset). if these circuits are disabled (lower current consumption), the reference voltage circuit requires a time to become stable before a low-voltage condition can be reliably detected. this time is invariant of system clock speed. this start-up time is specified in electrical specification parameter 36. the low-voltage interrupt flag will not be enabled until a stable reference voltage is reached. refer to the waveform in figure 21-4. 21.2.2 current consumption when the module is enabled, the lvd comparator and voltage divider are enabled and will consume static current. the voltage divider can be tapped from multiple places in the resistor array. total current consumption, when enabled, is specified in electrical specification parameter #d022b. 21.3 operation during sleep when enabled, the lvd circuitry continues to operate during sleep. if the device voltage crosses the trip point, the lvdif bit will be set and the device will wake- up from sleep. device execution will continue from the interrupt vector address if interrupts have been globally enabled. 21.4 effects of a reset a device reset forces all registers to their reset state. this forces the lvd module to be turned off.
pic18f2331/2431/4331/4431 ds39616b-page 266 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 267 pic18f2331/2431/4331/4431 22.0 special features of the cpu pic18f2331/2431/4331/4431 devices include several features intended to maximize system reliability and minimize cost through elimination of external compo- nents. these are:  oscillator selection  resets: - power-on reset (por) - power-up timer (pwrt) - oscillator start-up timer (ost) - brown-out reset (bor)  interrupts  watchdog timer (wdt)  fail-safe clock monitor  two-speed start-up  code protection  id locations  in-circuit serial programming? (icsp?) the oscillator can be configured for the application depending on frequency, power, accuracy and cost. all of the options are discussed in detail in section 2.0 ?oscillator configurations? . a complete discussion of device resets and interrupts is available in previous sections of this data sheet. in addition to their power-up and oscillator start-up tim- ers provided for resets, pic18f2331/2431/4331/4431 devices have a watchdog timer, which is either perma- nently enabled via the configuration bits, or software controlled (if configured as disabled). the inclusion of an internal rc oscillator also provides the additional benefits of a fail-safe clock monitor (fscm) and two-speed start-up. fscm provides for background monitoring of the peripheral clock and automatic switchover in the event of its failure. two- speed start-up enables code to be executed almost immediately on start-up, while the primary clock source completes its start-up delays. all of these features are enabled and configured by setting the appropriate configuration register bits. 22.1 configuration bits the configuration bits can be programmed (read as ? 0 ?), or left unprogrammed (read as ? 1 ?), to select various device configurations. these bits are mapped starting at program memory location 300000h. the user will note that address 300000h is beyond the user program memory space. in fact, it belongs to the configuration memory space (300000h-3fffffh), which can only be accessed using table reads and table writes. programming the configuration registers is done in a manner similar to programming the flash memory. the eecon1 register wr bit starts a self-timed write to the configuration register. in normal operation mode, a tblwt instruction with the tblptr pointing to the configuration register sets up the address and the data for the configuration register write. setting the wr bit starts a long write to the configuration register. the configuration registers are written a byte at a time. to write or erase a configuration cell, a tblwt instruction can write a ? 1 ? or a ? 0 ? into the cell. for additional details on flash programming, refer to section 6.5 ?writing to flash program memory? .
pic18f2331/2431/4331/4431 ds39616b-page 268 preliminary ? 2003 microchip technology inc. table 22-1: configuration bits and device ids register 22-1: config1h: configuration register 1 high (byte address 300001h) file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 default/ unprogrammed value 300000h config1l ? ? ? ? ? ? ? ? ---- ---- 300001h config1h ieso fcmen ? ? fosc3 fosc2 fosc1 fosc0 11-- 1111 300002h config2l ? ? ? ? borv1 borv0 boren pwrten ---- 1111 300003h config2h ? ? winen wdps3 wdps2 wdps1 wdps0 wdten ---1 1111 300004h config3l ? ?t1oscmx hpol lpol pwmpin ? ? --11 11-- 300005h config3h mclre ? ? exclkmx pwm4mx sspmx ?fltamx 1--1 1-11 300006h config4l debug ? ? ? ?lvp ?stvren 1--- -1-1 300007h config4h ? ? ? ? ? ? ? ? ---- ---- 300008h config5l ? ? ? ?cp3cp2cp1cp0 ---- 1111 300009h config5h cpd cpb ? ? ? ? ? ? 11-- ---- 30000ah config6l ? ? ? ? wrt3 wrt2 wrt1 wrt0 ---- 1111 30000bh config6h wrtd wrtb wrtc ? ? ? ? ? 111- ---- 30000ch config7l ? ? ? ? ebtr3 ebtr2 ebtr1 ebtr0 ---- 1111 30000dh config7h ?ebtrb ? ? ? ? ? ? -1-- ---- 3ffffeh devid1 (1) dev2 dev1 dev0 rev4 rev3 rev2 rev1 rev0 xxxx xxxx (1) 3fffffh devid2 (1) dev10 dev9 dev8 dev7 dev6 dev5 dev4 dev3 0000 0101 legend: x = unknown, u = unchanged, ? = unimplemented, q = value depends on condition. shaded cells are unimplemented, read as ? 0 ?. note 1: see register 22-13 for devid1 values. devid register s are read-only and cannot be programmed by the user. r/p-1 r/p-1 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 ieso fcmen ? ?f osc 3f osc 2f osc 1f osc 0 bit 7 bit 0 bit 7 ieso: internal external switch over bit 1 = internal external switch over mode enabled 0 = internal external switch over mode disabled bit 6 fcmen : fail-safe clock monitor enable bit 1 = fail-safe clock monitor enabled 0 = fail-safe clock monitor disabled bit 5-4 unimplemented: read as ? 0 ? bit 3-0 f osc <3:0>: oscillator selection bits 11xx = external rc oscillator, clko function on ra6 1001 = internal oscillator block, clko function on ra6, and port function on ra7 1000 = internal oscillator block, port function on ra6, and port function on ra7 0111 = external rc oscillator, port function on ra6 0110 = hs oscillator, pll enabled (clock frequency = 4 x f osc 1) 0101 = ec oscillator, port function on ra6 0100 = ec oscillator, clko function on ra6 0010 = hs oscillator 0001 = xt oscillator 0000 = lp oscillator legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogramme d u = unchanged from programmed state
? 2003 microchip technology inc. preliminary ds39616b-page 269 pic18f2331/2431/4331/4431 register 22-2: config2l: configuration register 2 low (byte address 300002h) u-0 u-0 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 ? ? ? ? borv1 borv0 boren pwrten bit 7 bit 0 bit 7-4 unimplemented: read as ? 0 ? bit 3-2 borv1:borv0: brown-out reset voltage bits 11 = reserved 10 = v bor set to 2.7v 01 = v bor set to 4.2v 00 = v bor set to 4.5v bit 1 boren: brown-out reset enable bit (1) 1 = brown-out reset enabled 0 = brown-out reset disabled bit 0 pwrten : power-up timer enable bit (1) 1 = pwrt disabled 0 = pwrt enabled note 1: having boren = 1 does not automatically override the pwrten to ? 0 ? nor automatically enable the power-up timer. legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
pic18f2331/2431/4331/4431 ds39616b-page 270 preliminary ? 2003 microchip technology inc. register 22-3: config2h: configuration register 2 high (byte address 300003h) u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 r/p-1 ? ?winen wdtps3 wdtps2 wdtps1 wdtps0 wdten bit 7 bit 0 bit 7-6 unimplemented: read as ? 0 ? bit 5 winen : watchdog timer window enable bit 1 = wdt window disabled 0 = wdt window enabled bit 4-1 wdps<3:0>: watchdog timer postscale select bits 1111 = 1:32,768 1110 = 1:16,384 1101 = 1:8,192 1100 = 1:4,096 1011 = 1:2,048 1010 = 1:1,024 1001 = 1:512 1000 = 1:256 0111 = 1:128 0110 = 1:64 0101 = 1:32 0100 = 1:16 0011 = 1:8 0010 = 1:4 0001 = 1:2 0000 = 1:1 bit 0 wdten: watchdog timer enable bit 1 = wdt enabled 0 = wdt disabled (control is placed on the swdten bit) legend: r = readable bit p = programmable bit u = unimplemented bit, read as ? 0 ? - n = value when device is unprogrammed u = unchanged from programmed state
? 2003 microchip technology inc. preliminary ds39616b-page 271 pic18f2331/2431/4331/4431 register 22-4: config3l: configuration register 3 low (byte address 300004h) u-0 u r/p-1 r/p-1 r/p-1 r/p-1 u u ? ?t1oscmx hpol lpol pwmpin ? ? bit 7 bit 0 bit 7-6 unimplemented : read as ? 0 ? bit 5 t1oscmx : timer1 oscillator mode bit 1 = low power timer1 operation when microcontroller is in sleep mode. 0 = standard (legacy) timer1 oscillator operation. bit 4 hpol (1 ) : high-side transistors polarity bit (i.e., odd pwm output polarity control bit ) 1 = pwm 1, 3, 5 and 7 are active-high (default) 0 = pwm 1, 3, 5 and 7 are active-low bit 3 lpol (1) : low-side transistors polarity bit (i.e., even pwm output polarity control bit) 1 = pwm 0, 2, 4 and 6 are active-high (default) 0 = pwm 0, 2, 4 and 6 are active-low bit 2 pwmpin (2) : pwm output pins reset state control bit 1 = pwm outputs disabled upon reset (default) 0 = pwm outputs drive active states upon reset (3) bit 1-0 unimplemented: read as ? 0 ? note 1: polarity control bits hpol and lpol define pwm signal output active and inactive states; pwm states generated by the fault inputs or pwm manual override. 2: pwm6 and pwm7 output channels are only available on the pic18f4x21 devices. 3: when pwmpin = 0, pwmen<2:0> = 101 if device has eight pwm output pins (40 and 44-pin devices) and pwmen<2:0> = 100 if the device has six pwm output pins (28-pin device). pwm output polarity is defined by hpol and lpol. legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
pic18f2331/2431/4331/4431 ds39616b-page 272 preliminary ? 2003 microchip technology inc. register 22-5: config3h: configuration register 3 high (byte address 300005h) r/p-1 u u r/p-1 r/p-1 r/p-1 u r/p-1 mclre ? ? exclkmx (1) pwm4mx (1) sspmx (1) ? fltamx (1) bit 7 bit 0 bit 7 mclre: mclr pin enable bit 1 = re3 input pin enabled; mclr disabled. 0 =mclr pin enabled: re3 input pin disabled. bit 6-5 unimplemented: read as ? 0 ? bit 4 exclkmx: tmr0/t5cki external clock mux bit 1 = tmr0/t5cki external clock input is multiplexed with rc3 0 = tmr0/t5cki external clock input is multiplexed with rd0 bit 3 pwm4mx: pwm4 mux bit 1 = pwm4 output is multiplexed with rb5 0 = pwm4 output is multiplexed with rd5 bit 2 sspmx: ssp i/o mux bit 1 = sck/scl clocks and sda/sdi data are multiplexed with rc5 and rc4 respectively. sdo output is multiplexed with rc7. 0 = sck/scl clocks and sda/sdi data are multiplexed with rd3 and rd2 respectively. sdo output is multiplexed with rd1. bit 1 unimplemented: read as ? 0 ? bit 0 fltamx: flta mux bit 1 =flta input is multiplexed with rc1 0 =flta input is multiplexed with rd4 note 1: unimplemented in pic18f2x31 devices; maintain this bit set. legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
? 2003 microchip technology inc. preliminary ds39616b-page 273 pic18f2331/2431/4331/4431 register 22-6: config4l: configuration register 4 low (byte address 300006h) r/p-1 u-0 u-0 u-0 u-0 r/p-1 u-0 r/p-1 debug ? ? ? ?lvp ?stvren bit 7 bit 0 bit 7 debug : background debugger enable bit 1 = background debugger disabled, rb6 and rb7 configured as general purpose i/o pins 0 = background debugger enabled, rb6 and rb7 are dedicated to in-circuit debug bit 6-3 unimplemented: read as ? 0 ? bit 2 lvp: low-voltage icsp enable bit 1 = low-voltage icsp enabled 0 = low-voltage icsp disabled bit 1 unimplemented: read as ? 0 ? bit 0 stvren: stack full/underflow reset enable bit 1 = stack full/underflow will cause reset 0 = stack full/underflow will not cause reset legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
pic18f2331/2431/4331/4431 ds39616b-page 274 preliminary ? 2003 microchip technology inc. register 22-7: config5l: configuration register 5 low (byte address 300008h) register 22-8: config5h: configuration register 5 high (byte address 300009h) u-0 u-0 u-0 u-0 r/c-1 r/c-1 r/c-1 r/c-1 ? ? ? ?cp3 (1) cp2 (1) cp1 cp0 bit 7 bit 0 bit 7-4 unimplemented: read as ? 0 ? bit 3 cp3: code protection bit 1 = block 3 (001800-001fffh) not code-protected 0 = block 3 (001800-001fffh) code-protected bit 2 cp2: code protection bit 1 = block 2 (001000-0017ffh) not code-protected 0 = block 2 (001000-0017ffh) code-protected bit 1 cp1: code protection bit 1 = block 1 (000800-000fffh) not code-protected 0 = block 1 (000800-000fffh) code-protected bit 0 cp0: code protection bit 1 = block 0 (000200-0007ffh) not code-protected 0 = block 0 (000200-0007ffh) code-protected note 1: unimplemented in pic18f2x31 devices; maintain this bit set. legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state r/c-1 r/c-1 u-0 u-0 u-0 u-0 u-0 u-0 cpd cpb ? ? ? ? ? ? bit 7 bit 0 bit 7 cpd: data eeprom code protection bit 1 = data eeprom not code-protected 0 = data eeprom code-protected bit 6 cpb: boot block code protection bit 1 = boot block (000000-0001ffh) not code-protected 0 = boot block (000000-0001ffh) code-protected bit 5-0 unimplemented: read as ? 0 ? legend: r = readable bit c = clearable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
? 2003 microchip technology inc. preliminary ds39616b-page 275 pic18f2331/2431/4331/4431 register 22-9: config6l: configuration register 6 low (byte address 30000ah) register 22-10: config6h: configuration register 6 high (byte address 30000bh) u-0 u-0 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 ? ? ? ?wrt3 (1) wrt2 (1) wrt1 wrt0 bit 7 bit 0 bit 7-4 unimplemented: read as ? 0 ? bit 3 wrt3: write protection bit (1) 1 = block 3 (001800-001fffh) not write-protected 0 = block 3 (001800-001fffh) write-protected bit 2 wrt2: write protection bit (1) 1 = block 2 (001000-0017ffh) not write-protected 0 = block 2 (001000-0017ffh) write-protected bit 1 wrt1: write protection bit 1 = block 1 (000800-000fffh) not write-protected 0 = block 1 (000800-000fffh) write-protected bit 0 wrt0: write protection bit 1 = block 0 (000200-0007ffh) not write-protected 0 = block 0 (000200-0007ffh) write-protected note 1: unimplemented in pic18f2x31 devices; maintain this bit set. legend: r = readable bit p = programmable bit u = unimplemented bit, read as ? 0 ? - n = value when device is unprogrammed u = unchanged from programmed state r/p-1 r/p-1 r-1 u-0 u-0 u-0 u-0 u-0 wrtd wrtb wrtc ? ? ? ? ? bit 7 bit 0 bit 7 wrtd: data eeprom write protection bit 1 = data eeprom not write-protected 0 = data eeprom write-protected bit 6 wrtb: boot block write protection bit 1 = boot block (000000-0001ffh) not write-protected 0 = boot block (000000-0001ffh) write-protected bit 5 wrtc: configuration register write protection bit 1 = configuration registers (300000-3000ffh) not write-protected 0 = configuration registers (300000-3000ffh) write-protected note: this bit is read-only in normal execution mode; it can be written only in program mode. bit 4-0 unimplemented: read as ? 0 ? legend: r = readable bit p = programmable bit u = unimplemented bit, read as ? 0 ? - n = value when device is unprogrammed u = unchanged from programmed state
pic18f2331/2431/4331/4431 ds39616b-page 276 preliminary ? 2003 microchip technology inc. register 22-11: config7l: conf iguration register 7 low (byte address 30000ch) register 22-12: config7h: configuration register 7 high (byte address 30000dh) u-0 u-0 u-0 u-0 r/p-1 r/p-1 r/p-1 r/p-1 ? ? ? ? ebtr3 (1) ebtr2 (1) ebtr1 ebtr0 bit 7 bit 0 bit 7-4 unimplemented: read as ? 0 ? bit 3 ebtr3: table read protection bit (1) 1 = block 3 (001800-001fffh) not protected from table reads executed in other blocks 0 = block 3 (001800-001fffh) protected from table reads executed in other blocks bit 2 ebtr2: table read protection bit (1) 1 = block 2 (001000-0017ffh) not protected from table reads executed in other blocks 0 = block 2 (001000-0017ffh) protected from table reads executed in other blocks bit 1 ebtr1: table read protection bit 1 = block 1 (000800-000fffh) not protected from table reads executed in other blocks 0 = block 1 (000800-000fffh) protected from table reads executed in other blocks bit 0 ebtr0: table read protection bit 1 = block 0 (000200-0007ffh) not protected from table reads executed in other blocks 0 = block 0 (000200-0007ffh) protected from table reads executed in other blocks note 1: unimplemented in pic18f2x31 devices; maintain this bit set. legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state u-0 r/p-1 u-0 u-0 u-0 u-0 u-0 u-0 ? ebtrb ? ? ? ? ? ? bit 7 bit 0 bit 7 unimplemented: read as ? 0 ? bit 6 ebtrb: boot block table read protection bit 1 = boot block (000000-0001ffh) not protected from table reads executed in other blocks 0 = boot block (000000-0001ffh) protected from table reads executed in other blocks bit 5-0 unimplemented: read as ? 0 ? legend: r = readable bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
? 2003 microchip technology inc. preliminary ds39616b-page 277 pic18f2331/2431/4331/4431 register 22-13: device id register 1 for pic18f2331/2431/4331/4431 devices register 22-14: device id register 2 for pic18f2331/2431/4331/4431 devices rrrrrrrr dev2 dev1 dev0 rev4 rev3 rev2 rev1 rev0 bit 7 bit 0 bit 7-5 dev<2:0>: device id bits these bits are used with the dev<10:3> bits in the device id register 2 to identify the part number. 000 = pic18f4331 001 = pic18f4431 100 = pic18f2331 101 = PIC18F2431 bit 4-0 rev<4:0>: revision id bits these bits are used to indicate the device revision. legend: r = read-only bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state rrrrrrrr dev10 dev9 dev8 dev7 dev6 dev5 dev4 dev3 bit 7 bit 0 bit 7-0 dev10:dev3: device id bits these bits are used with the dev2:dev0 bits in the device id register 1 to identify the part number 0000 0101 = pic18f2331/2431/4331/4431 devices note 1: these values for dev10:dev3 may be shared with other devices. the specific device is always identified by using the entire dev10:dev0 bit sequence. legend: r = read-only bit p = programmable bit u = unimplemented bit, read as ?0? - n = value when device is unprogrammed u = unchanged from programmed state
pic18f2331/2431/4331/4431 ds39616b-page 278 preliminary ? 2003 microchip technology inc. 22.2 watchdog timer (wdt) for pic18f2331/2431/4331/4431 devices, the wdt is driven by the intrc source. when the wdt is enabled, the clock source is also enabled. the nominal wdt period is 4 ms and has the same stability as the intrc oscillator. the 4 ms period of the wdt is multiplied by a 16-bit postscaler. any output of the wdt postscaler is selected by a multiplexer, controlled by bits in configuration register 2h (see register 22-3). available periods range from 4 ms to 131.072 seconds (2.18 minutes). the wdt and postscaler are cleared when any of the following events occur: execute a sleep or clrwdt instruction, the ircf bits (osccon<6:4>) are changed, or a clock failure has occurred (see section 22.4.1 ?fscm and the watchdog timer? ). adjustments to the internal oscillator clock period using the osctune register also affect the period of the wdt by the same factor. for example, if the intrc period is increased by 3%, then the wdt period is increased by 3%. 22.2.1 control register register 22-15 shows the wdtcon register. this is a readable and writable register. the swdten bit allows software to enable or disable the wdt, but only if the configuration bit has disabled the wdt. the wdtw bit is a read-only bit that indicates when the wdt count is in the fourth quadrant (i.e., when the 8-bit wdt value is b ? 11000000 ? or greater). figure 22-1: wdt block diagram register 22-15: wdtcon register note 1: the clrwdt and sleep instructions clear the wdt and postscaler counts when executed. 2: changing the setting of the ircf bits (osccon<6:4> clears the wdt and postscaler counts. 3: when a clrwdt instruction is executed the postscaler count will be cleared. 4: if winen = 0 , then clrwdt must be executed only when wdtw = 1 ; other- wise, a device reset will result. r-0 u-0 u-0 u-0 u-0 u-0 u-0 r/w-0 wdtw ? ? ? ? ? ?swdten bit 7 bit 0 bit 7 wdtw: watchdog timer window bit 1 = wdt count is in fourth quadrant 0 = wdt count is not in fourth quadrant bit 6 unimplemented bit 0 swdten: software enable / disable for watch dog timer bit (1) 1 = wdt is turned on 0 = wdt is turned off note 1: if wdten configuration bit = 1 , then wdt is always enabled, irrespective of this control bit. if wdten configuration bit = 0 , then it is possible to turn wdt on/off with this con- trol bit. legend: r = readable bit w = writable bit u = unimplemented bit, read as ?0? - n = value at por intrc source wdt wake-up reset wdt wdt counter programmable postscaler 1:1 to 1:32,768 enable wdt wdtps<3:0> swdten wdten clrwdt 4 from sleep reset all device resets sleep intrc control 125 change on ircf bits
? 2003 microchip technology inc. preliminary ds39616b-page 279 pic18f2331/2431/4331/4431 table 22-2: summary of watchdog timer registers 22.3 two-speed start-up the two-speed start-up feature helps to minimize the latency period from oscillator start-up to code execution by allowing the microcontroller to use the intrc oscil- lator as a clock source until the primary clock source is available. it is enabled by setting the ieso bit in configuration register 1h (config1h<7>). two-speed start-up is available only if the primary oscillator mode is lp, xt, hs or hspll (crystal-based modes). other sources do not require a ost start-up delay; for these, two-speed start-up is disabled. when enabled, resets and wake-ups from sleep mode cause the device to configure itself to run from the inter- nal oscillator block as the clock source, following the time-out of the power-up timer after a por reset is enabled. this allows almost immediate code execu- tion, while the primary oscillator starts and the ost is running. once the ost times out, the device automat- ically switches to pri_run mode. because the osccon register is cleared on reset events, the intosc (or postscaler) clock source is not initially available after a reset event; the intrc clock is used directly at its base frequency. to use a higher clock speed on wake-up, the intosc or postscaler clock sources can be selected to provide a higher clock speed by setting bits ifrc2:ifrc0 immediately after reset. for wake-ups from sleep, the intosc or postscaler clock sources can be selected by setting ifrc2:ifrc0 prior to entering sleep mode. in all other power-managed modes, two-speed start- up is not used. the device will be clocked by the cur- rently selected clock source until the primary clock source becomes available. the setting of the ieso bit is ignored. 22.3.1 special considerations for using two-speed start-up while using the intrc oscillator in two-speed start- up, the device still obeys the normal command sequences for entering power-managed modes, including serial sleep instructions (refer to section 3.1.3 ?multiple sleep commands? ). in prac- tice, this means that user code can change the scs1:scs0 bit settings and issue sleep commands before the ost times out. this would allow an applica- tion to briefly wake-up, perform routine ?housekeeping? tasks and return to sleep before the device starts to operate from the primary oscillator. user code can also check if the primary clock source is currently providing the system clocking by checking the status of the osts bit (osccon<3>). if the bit is set, the primary oscillator is providing the system clock. otherwise, the internal oscillator block is providing the clock during wake-up from reset or sleep mode. name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 config2h ? ? winen wdtps3 wdtps2 wdtps2 wdtps0 wdten rcon ipen ? ? ri to pd por bor wdtcon wdtw ? ? ? ? ? ? swdten legend: shaded cells are not used by the watchdog timer.
pic18f2331/2431/4331/4431 ds39616b-page 280 preliminary ? 2003 microchip technology inc. figure 22-2: timing transition for two-speed start-up (intosc to hspll) q1 q3 q4 osc1 peripheral program pc pc + 2 intosc pll clock q1 pc + 6 q2 output q3 q4 q1 cpu clock pc + 4 clock counter q2 q2 q3 q4 note 1: t ost = 1024 t osc ; t pll = 2 ms (approx). these intervals are not shown to scale. wake from interrupt event t ost (1) t pll (1) 12345678 clock transition osts bit set multiplexer
? 2003 microchip technology inc. preliminary ds39616b-page 281 pic18f2331/2431/4331/4431 22.4 fail-safe clock monitor the fail-safe clock monitor (fscm) allows the microcontroller to continue operation in the event of an external oscillator failure, by automatically switching the system clock to the internal oscillator block. the fscm function is enabled by setting the fail-safe clock monitor enable bit, fcmen (config1h<6>). when fscm is enabled, the intrc oscillator runs at all times to monitor clocks to peripherals and provide an instant backup clock in the event of a clock failure. clock monitoring (shown in figure 22-3) is accomplished by creating a sample clock signal, which is the intrc output divided by 64. this allows ample time between fscm sample clocks for a peripheral clock edge to occur. the peripheral system clock and the sample clock are presented as inputs to the clock monitor latch (cm). the cm is set on the falling edge of the system clock source, but cleared on the rising edge of the sample clock. figure 22-3: fscm block diagram clock failure is tested for on the falling edge of the sample clock. if a sample clock falling edge occurs while cm is still set, a clock failure has been detected (figure 22-4). this causes the following:  the fscm generates an oscillator fail interrupt by setting bit oscfif (pir2<7>);  the system clock source is switched to the internal oscillator block (osccon is not updated to show the current clock source ? this is the fail-safe condition); and the wdt is reset. since the postscaler frequency from the internal oscillator block may not be sufficiently stable, it may be desirable to select another clock configuration and enter an alternate power-managed mode (see section 22.3.1 ?special considerations for using two-speed start-up? and section 3.1.3 ?multiple sleep commands? for more details). this can be done to attempt a partial recovery or execute a controlled shutdown. to use a higher clock speed on wake-up, the intosc or postscaler clock sources can be selected to provide a higher clock speed by setting bits ifrc2:ifrc0 immediately after reset. for wake-ups from sleep, the intosc or postscaler clock sources can be selected by setting ifrc2:ifrc0 prior to entering sleep mode. adjustments to the internal oscillator block using the osctune register also affect the period of the fscm by the same factor. this can usually be neglected, as the clock frequency being monitored is generally much higher than the sample clock frequency. the fscm will detect failures of the primary or second- ary clock sources only. if the internal oscillator block fails, no failure would be detected, nor would any action be possible. 22.4.1 fscm and the watchdog timer both the fscm and the wdt are clocked by the intrc oscillator. since the wdt operates with a separate divider and counter, disabling the wdt has no effect on the operation of the intrc oscillator when the fscm is enabled. as already noted, the clock source is switched to the intosc clock when a clock failure is detected. depending on the frequency selected by the ircf2:ircf0 bits, this may mean a substantial change in the speed of code execution. if the wdt is enabled with a small prescale value, a decrease in clock speed allows a wdt time-out to occur, and a subsequent device reset. for this reason, fail-safe clock events also reset the wdt and postscaler, allowing it to start timing from when execution speed was changed and decreasing the likelihood of an erroneous time-out. 22.4.2 exiting fail-safe operation the fail-safe condition is terminated by either a device reset, or by entering a power-managed mode. on reset, the controller starts the primary clock source specified in configuration register 1h (with any required start-up delays that are required for the oscillator mode, such as ost or pll timer). the intosc multiplexer provides the system clock until the primary clock source becomes ready (similar to a two-speed start-up). the clock system source is then switched to the primary clock (indicated by the osts bit in the osccon register becoming set). the fail-safe clock monitor then resumes monitoring the peripheral clock. the primary clock source may never become ready during start-up. in this case, operation is clocked by the intosc multiplexer. the osccon register will remain in its reset state until a power-managed mode is entered. entering a power-managed mode by loading the osccon register and executing a sleep instruction will clear the fail-safe condition. when the fail-safe condition is cleared, the clock monitor will resume monitoring the peripheral clock. peripheral intrc 64 s c q (32 s) 488 hz (2.048 ms) clock monitor latch (cm) (edge-triggered) cloc k failur e detecte d source clock q
pic18f2331/2431/4331/4431 ds39616b-page 282 preliminary ? 2003 microchip technology inc. figure 22-4: fscm timing diagram 22.4.3 fscm interrupts in power- managed modes as previously mentioned, entering a power-managed mode clears the fail-safe condition. by entering a power-managed mode, the clock multiplexer selects the clock source selected by the osccon register. fail-safe monitoring of the power-managed clock source resumes in the power-managed mode. if an oscillator failure occurs during power-managed operation, the subsequent events depend on whether or not the oscillator failure interrupt is enabled. if enabled (oscfif = 1 ), code execution will be clocked by the intosc multiplexer. an automatic transition back to the failed clock source will not occur. if the interrupt is disabled, the device will not exit the power-managed mode on oscillator failure. instead, the device will continue to operate as before, but clocked by the intosc multiplexer. while in idle mode, subse- quent interrupts will cause the cpu to begin executing instructions while being clocked by the intosc multi- plexer. the device will not transition to a different clock source until the fail-safe condition is cleared. 22.4.4 por or wake from sleep the fscm is designed to detect oscillator failure at any point after the device has exited power-on reset (por) or low-power sleep mode. when the primary system clock is ec, rc or intrc modes, monitoring can begin immediately following these events. for oscillator modes involving a crystal or resonator (hs, hspll, lp or xt), the situation is somewhat different. since the oscillator may require a start-up time considerably longer than the fcsm sample clock time, a false clock failure may be detected. to prevent this, the internal oscillator block is automatically configured as the system clock and functions until the primary clock is stable (the ost and pll timers have timed out). this is identical to two-speed start-up mode. once the primary clock is stable, the intrc returns to its role as the fscm source. as noted in section 22.3.1 ?special considerations for using two-speed start-up? , it is also possible to select another clock configuration and enter an alternate power-managed mode, while waiting for the primary system clock to become stable. when the new powered managed mode is selected, the primary clock is disabled. oscfif cm output system clock output sample clock failure detected oscillator failure note: the system clock is normally at a much higher frequency than the sample clock. the relative frequencies in this example have been chosen for clarity. (q ) cm test cm test cm test note: the same logic that prevents false oscillator failure interrupts on por or wake from sleep will also prevent the detection of the oscillator?s failure to start at all following these events. this can be avoided by monitoring the osts bit and using a timing routine to determine if the oscillator is taking too long to start. even so, no oscillator failure interrupt will be flagged.
? 2003 microchip technology inc. preliminary ds39616b-page 283 pic18f2331/2431/4331/4431 22.5 program verification and code protection the overall structure of the code protection on the pic18 flash devices differs significantly from other picmicro ? devices. the user program memory is divided into five blocks. one of these is a boot block of 512 bytes. the remainder of the memory is divided into four blocks on binary boundaries. each of the five blocks has three code protection bits associated with them. they are:  code-protect bit (cpn)  write-protect bit (wrtn)  external block table read bit (ebtrn) figure 22-5 shows the program memory organization for 8- and 16-kbyte devices, and the specific code protection bit associated with each block. the actual locations of the bits are summarized in table 22-3. figure 22-5: code-protected program memory for pic18f2331/2431/4331/4431 table 22-3: summary of code protection registers memory size/device block code protection controlled by: 8kbytes (pic18fx331) address range 16 kbytes (pic18fx431) address range boot block 0000h 0fffh boot block 0000h 01ffh cpb, wrtb, ebtrb block 0 0200h 0fffh block 0 0200h 0fffh cp0, wrt0, ebtr0 block 1 1000h 1fffh block 1 1000h 1fffh cp1, wrt1, ebtr1 unimplemented read 0 ?s block 2 2000h 2fffh cp2, wrt2, ebtr2 3fffh block 3 3000h 3fffh cp3, wrt3, ebtr3 file name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 300008h config5l ? ? ? ? cp3 cp2 cp1 cp0 300009h config5h cpd cpb ? ? ? ? ? ? 30000ah config6l ? ? ? ? wrt3 wrt2 wrt1 wrt0 30000bh config6h wrtd wrtb wrtc ? ? ? ? ? 30000ch config7l ? ? ? ? ebtr3 ebtr2 ebtr1 ebtr0 30000dh config7h ? ebtrb ? ? ? ? ? ? legend: shaded cells are unimplemented.
pic18f2331/2431/4331/4431 ds39616b-page 284 preliminary ? 2003 microchip technology inc. 22.5.1 program memory code protection the program memory may be read to or written from any location using the table read and table write instructions. the device id may be read with table reads. the configuration registers may be read and written with the table read and table write instructions. in normal execution mode, the cpn bits have no direct effect. cpn bits inhibit external reads and writes. a block of user memory may be protected from table writes if the wrtn configuration bit is ? 0 ?. the ebtrn bits control table reads. for a block of user memory with the ebtrn bit set to ? 0 ?, a table read instruction that executes from within that block is allowed to read. a table read instruc- tion that executes from a location outside of that block is not allowed to read, and will result in reading ? 0 ?s. figures 22-6 through 22-8 illustrate table write and table read protection. figure 22-6: table write (wrtn) disallowed note: code protection bits may only be written to a ? 0 ? from a ? 1 ? state. it is not possible to write a ? 1 ? to a bit in the ? 0 ? state. code protection bits are only set to ? 1 ? by a full chip erase or block erase function. the full chip erase and block erase functions can only be initiated via icsp or an external programmer. 000000h 0001ffh 000200h 0007ffh 000800h 000fffh 001000h 0017ffh 001800h 001fffh wrtb,ebtrb = 11 wrt0,ebtr0 = 01 wrt1,ebtr1 = 11 wrt2,ebtr2 = 11 wrt3,ebtr3 = 11 tblwt * tblptr = 0002ffh pc = 0007feh tblwt * pc = 0017feh register values program memory configuration bit settings results: all table writes disabled to blockn whenever wrtn = ? 0 ? .
? 2003 microchip technology inc. preliminary ds39616b-page 285 pic18f2331/2431/4331/4431 figure 22-7: external block t able read (ebtrn) disallowed figure 22-8: external block table read (ebtrn) allowed 000000h 0001ffh 000200h 0007ffh 000800h 000fffh 001000h 0017ffh 001800h 001fffh wrtb,ebtrb = 11 wrt0,ebtr0 = 10 wrt1,ebtr1 = 11 wrt2,ebtr2 = 11 wrt3,ebtr3 = 11 tblrd * tblptr = 0002ffh pc = 000ffeh results: all table reads from external blocks to blockn are disabled whenever ebtrn = ? 0 ?. tablat register returns a value of ? 0 ?. register values program memory configuration bit settings 000000h 0001ffh 000200h 0007ffh 000800h 000fffh 001000h 0017ffh 001800h 001fffh wrtb,ebtrb = 11 wrt0,ebtr0 = 10 wrt1,ebtr1 = 11 wrt2,ebtr2 = 11 wrt3,ebtr3 = 11 tblrd * tblptr = 0002ffh pc = 0007feh register values program memory configuration bit settings results: table reads permitted within blockn, even when ebtrbn = ? 0 ?. tablat register returns the value of the data at the location tblptr.
pic18f2331/2431/4331/4431 ds39616b-page 286 preliminary ? 2003 microchip technology inc. 22.5.2 data eeprom code protection the entire data eeprom is protected from external reads and writes by two bits: cpd and wrtd. cpd inhibits external reads and writes of data eeprom. wrtd inhibits external writes to data eeprom. the cpu can continue to read and write data eeprom regardless of the protection bit settings. 22.5.3 configuration register protection the configuration registers can be write-protected. the wrtc bit controls protection of the configuration registers. in normal execution mode, the wrtc bit is readable only. wrtc can only be written via icsp or an external programmer. 22.6 id locations eight memory locations (200000h-200007h) are designated as id locations, where the user can store checksum or other code identification numbers. these locations are both readable and writable during normal execution through the tblrd and tblwt instructions, or during program/verify. the id locations can be read when the device is code-protected. 22.7 in-circuit serial programming pic18f2331/2431/4331/4431 microcontrollers can be serially programmed while in the end application circuit. this is simply done with two lines for clock and data, and three other lines for power, ground and the pro- gramming voltage. this allows customers to manufac- ture boards with unprogrammed devices, and then program the microcontroller just before shipping the product. this also allows the most recent firmware or a custom firmware to be programmed. 22.8 in-circuit debugger when the debug bit in configuration register config4l is programmed to a ? 0 ?, the in-circuit debugger functionality is enabled. this function allows simple debugging functions when used with mplab ? ide. when the microcontroller has this feature enabled, some resources are not available for general use. table 22-4 shows which resources are required by the background debugger. table 22-4: debugger resources to use the in-circuit debugger function of the micro- controller, the design must implement in-circuit serial programming connections to mclr /v pp , v dd , v ss , rb7 and rb6. this will interface to the in-circuit debugger module available from microchip or one of the third party development tool companies. 22.9 low-voltage icsp programming the lvp bit in configuration register 4l (config4l<2>) enables low-voltage icsp program- ming (lvp). when lvp is enabled, the microcontroller can be programmed without requiring high voltage being applied to the mclr /v pp pin, but the rb5/pgm pin is then dedicated to controlling program mode entry and is not available as a general purpose i/o pin. lvp is enabled in erased devices. while programming using lvp, v dd is applied to the mclr /v pp pin as in normal execution mode. to enter programming mode, v dd is applied to the pgm pin. if low-voltage icsp programming mode will not be used, the lvp bit can be cleared and rb5/pgm becomes available as the digital i/o pin rb5. the lvp bit may be set or cleared only when using standard high voltage programming (v ihh applied to the mclr /v pp pin). once lvp has been disabled, only the standard high voltage programming is available and must be used to program the device. memory that is not code-protected can be erased using either a block erase, or erased row by row, then written at any specified v dd . if code-protected memory is to be erased, a block erase is required. if a block erase is to be performed when using low-voltage programming, the device must be supplied with v dd of 4.5v to 5.5v. i/o pins: rb6, rb7 stack: 2 levels program memory: 512 bytes data memory: 10 bytes note 1: high voltage programming is always available, regardless of the state of the lvp bit or the pgm pin, by applying v ihh to the mclr pin. 2: when low-voltage programming is enabled, the rb5 pin can no longer be used as a general purpose i/o pin. 3: when lvp is enabled, externally pull the pgm pin to v ss to allow normal program execution.
? 2003 microchip technology inc. preliminary ds39616b-page 287 pic18f2331/2431/4331/4431 23.0 instruction set summary the pic18 instruction set adds many enhancements to the previous picmicro instruction sets, while maintain- ing an easy migration from these picmicro instruction sets. most instructions are a single program memory word (16-bits), but there are three instructions that require two program memory locations. each single-word instruction is a 16-bit word divided into an opcode, which specifies the instruction type and one or more operands, which further specify the operation of the instruction. the instruction set is highly orthogonal and is grouped into four basic categories:  byte-oriented operations  bit-oriented operations  literal operations  control operations the pic18 instruction set summary in table 23-2 lists byte-oriented , bit-oriented , literal and control opera- tions. table 23-1 shows the opcode field descriptions. most byte-oriented instructions have three operands: 1. the file register (specified by ?f?) 2. the destination of the result (specified by ?d?) 3. the accessed memory (specified by ?a?) the file register designator 'f' specifies which file register is to be used by the instruction. the destination designator ?d? specifies where the result of the operation is to be placed. if 'd' is zero, the result is placed in the wreg register. if 'd' is one, the result is placed in the file register specified in the instruction. all bit-oriented instructions have three operands: 1. the file register (specified by ?f?) 2. the bit in the file register (specified by ?b?) 3. the accessed memory (specified by ?a?) the bit field designator 'b' selects the number of the bit affected by the operation, while the file register desig- nator 'f' represents the number of the file in which the bit is located. the literal instructions may use some of the following operands:  a literal value to be loaded into a file register (specified by ?k?)  the desired fsr register to load the literal value into (specified by ?f?)  no operand required (specified by ???) the control instructions may use some of the following operands:  a program memory address (specified by ?n?)  the mode of the call or return instructions (specified by ?s?)  the mode of the table read and table write instructions (specified by ?m?)  no operand required (specified by ???) all instructions are a single word, except for three dou- ble word instructions. these three instructions were made double word instructions so that all the required information is available in these 32 bits. in the second word, the 4 msbs are 1?s. if this second word is executed as an instruction (by itself), it will execute as a nop . all single-word instructions are executed in a single instruction cycle, unless a conditional test is true or the program counter is changed as a result of the instruc- tion. in these cases, the execution takes two instruction cycles with the additional instruction cycle(s) executed as a nop . the double word instructions execute in two instruction cycles. one instruction cycle consists of four oscillator periods. thus, for an oscillator frequency of 4 mhz, the normal instruction execution time is 1 s. if a conditional test is true or the program counter is changed as a result of an instruction, the instruction execution time is 2 s. two- word branch instructions (if true) would take 3 s. figure 23-1 shows the general formats that the instruc- tions can have. all examples use the format ? nnh ? to represent a hexa- decimal number, where ? h ? signifies a hexadecimal digit. the instruction set summary, shown in table 23-2, lists the instructions recognized by the microchip assembler (mpasm tm assembler). section 23.2 ?instruction set? provides a description of each instruction. 23.1 read-modify-write operations any instruction that specifies a file register as part of the instruction performs a read-modify-write (r-m-w) operation. the register is read, the data is modified, and the result is stored according to either the instruc- tion or the destination designator ?d?. a read operation is performed on a register even if the instruction writes to that register. for example, a " bcf portb,1 " instruction will read portb, clear bit 1 of the data, then write the result back to portb. the read operation would have the unintended result that any condition that sets the rbif flag would be cleared. the r-m-w operation may also copy the level of an input pin to its corresponding output latch.
pic18f2331/2431/4331/4431 ds39616b-page 288 preliminary ? 2003 microchip technology inc. table 23-1: opcode field descriptions field description a ram access bit: a = 0 : ram location in access ram (bsr register is ignored) a = 1 : ram bank is specified by bsr register bbb bit address within an 8-bit file register (0 to 7). bsr bank select register. used to select the current ram bank. d destination select bit: d = 0 : store result in wreg d = 1 : store result in file register f dest destination either the wreg register or the specified register file location. f 8-bit register file address (0x00 to 0xff). fs 12-bit register file address (0x000 to 0xfff). this is the source address. fd 12-bit register file address (0x000 to 0xfff). this is the destination address. k literal field, constant data or label (may be either an 8-bit, 12-bit or a 20-bit value). label label name. mm the mode of the tblptr register for the table read and table write instructions. only used with table read and table write instructions: * no change to register (such as tb lptr with table reads and writes). *+ post-increment register (such as tblptr with table reads and writes). *- post-decrement register (such as tblptr with table reads and writes). +* pre-increment register (such as tblptr with table reads and writes). n the relative address (2?s complement number) for relative branch instructions, or the direct address for call/ branch and return instructions. prodh product of multiply high byte. prodl product of multiply low byte. s fast call/return mode select bit: s = 0 : do not update into/from shadow registers s = 1 : certain registers loaded into/from shadow registers (fast mode) u unused or unchanged. wreg working register (accumulator). x don't care ( 0 or 1 ) . the assembler will generate code with x = 0 . it is the recommended form of use for compatibility with all microchip software tools. tblptr 21-bit table pointer (points to a program memory location). tablat 8-bit table latch. tos top-of-stack. pc program counter. pcl program counter low byte. pch program counter high byte. pclath program counter high byte latch. pclatu program counter upper byte latch. gie global interrupt enable bit. wdt watchdog timer. to time-out bit. pd power-down bit. c, dc, z, ov, n alu status bits carry, digit carry, zero, overflow, negative. [ ] optional. ( ) contents. assigned to. < > register bit field. in the set of. italics user defined term (font is courier).
? 2003 microchip technology inc. preliminary ds39616b-page 289 pic18f2331/2431/4331/4431 figure 23-1: general format for instructions byte-oriented file register operations 15 10 9 8 7 0 d = 0 for result destination to be wreg register opcode d a f (file #) d = 1 for result destination to be file register (f) a = 0 to force access bank bit-oriented file register operations 15 12 11 9 8 7 0 opcode b (bit #) a f (file #) b = 3-bit position of bit in file register (f) literal operations 15 8 7 0 opcode k (literal) k = 8-bit immediate value byte to byte move operations (2-word) 15 12 11 0 opcode f (source file #) call, goto and branch operations 15 8 7 0 opcode n<7:0> (literal) n = 20-bit immediate value a = 1 for bsr to select bank f = 8-bit file register address a = 0 to force access bank a = 1 for bsr to select bank f = 8-bit file register address 15 12 11 0 1111 n<19:8> (literal) 15 12 11 0 1111 f (destination file #) f = 12-bit file register address control operations example instruction addwf myreg, w, b movff myreg1, myreg2 bsf myreg, bit, b movlw 0x7f goto label 15 8 7 0 opcode n<7:0> (literal) 15 12 11 0 n<19:8> (literal) call myfunc 15 11 10 0 opcode n<10:0> (literal) s = fast bit bra myfunc 15 8 7 0 opcode n<7:0> (literal) bc myfunc s
pic18f2331/2431/4331/4431 ds39616b-page 290 preliminary ? 2003 microchip technology inc. table 23-2: pic18fxxx instruction set mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb byte-oriented file register operations addwf addwfc andwf clrf comf cpfseq cpfsgt cpfslt decf decfsz dcfsnz incf incfsz infsnz iorwf movf movff movwf mulwf negf rlcf rlncf rrcf rrncf setf subfwb subwf subwfb swapf tstfsz xorwf f, d, a f, d, a f, d, a f, a f, d, a f, a f, a f, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f, d, a f s , f d f, a f, a f, a f, d, a f, d, a f, d, a f, d, a f, a f, d, a f, d, a f, d, a f, d, a f, a f, d, a add wreg and f add wreg and carry bit to f and wreg with f clear f complement f compare f with wreg, skip = compare f with wreg, skip > compare f with wreg, skip < decrement f decrement f, skip if 0 decrement f, skip if not 0 increment f increment f, skip if 0 increment f, skip if not 0 inclusive or wreg with f move f move f s (source) to 1st word f d (destination) 2nd word move wreg to f multiply wreg with f negate f rotate left f through carry rotate left f (no carry) rotate right f through carry rotate right f (no carry) set f subtract f from wreg with borrow subtract wreg from f subtract wreg from f with borrow swap nibbles in f test f, skip if 0 exclusive or wreg with f 1 1 1 1 1 1 (2 or 3) 1 (2 or 3) 1 (2 or 3) 1 1 (2 or 3) 1 (2 or 3) 1 1 (2 or 3) 1 (2 or 3) 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 (2 or 3) 1 0010 0010 0001 0110 0001 0110 0110 0110 0000 0010 0100 0010 0011 0100 0001 0101 1100 1111 0110 0000 0110 0011 0100 0011 0100 0110 0101 0101 0101 0011 0110 0001 01da 00da 01da 101a 11da 001a 010a 000a 01da 11da 11da 10da 11da 10da 00da 00da ffff ffff 111a 001a 110a 01da 01da 00da 00da 100a 01da 11da 10da 10da 011a 10da ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff c, dc, z, ov, n c, dc, z, ov, n z, n z z, n none none none c, dc, z, ov, n none none c, dc, z, ov, n none none z, n z, n none none none c, dc, z, ov, n c, z, n z, n c, z, n z, n none c, dc, z, ov, n c, dc, z, ov, n c, dc, z, ov, n none none z, n 1, 2 1, 2 1,2 2 1, 2 4 4 1, 2 1, 2, 3, 4 1, 2, 3, 4 1, 2 1, 2, 3, 4 4 1, 2 1, 2 1 1, 2 1, 2 1, 2 1, 2 4 1, 2 bit-oriented file register operations bcf bsf btfsc btfss btg f, b, a f, b, a f, b, a f, b, a f, d, a bit clear f bit set f bit test f, skip if clear bit test f, skip if set bit toggle f 1 1 1 (2 or 3) 1 (2 or 3) 1 1001 1000 1011 1010 0111 bbba bbba bbba bbba bbba ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff none none none none none 1, 2 1, 2 3, 4 3, 4 1, 2 note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is ? 1 ? for a pin configured as input and is driven low by an external device, the data will be written back with a ? 0 ?. 2: if this instruction is executed on the tmr0 register (and, where applicable, d = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are 2-word instructions. the second word of these instructions will be executed as a nop , unless the first word of the instruction retrieves the information embedded in these 16 bits. this ensures that all program memory locations have a valid instruction. 5: if the table write starts the write cycle to internal memory, the write will continue until terminated.
? 2003 microchip technology inc. preliminary ds39616b-page 291 pic18f2331/2431/4331/4431 control operations bc bn bnc bnn bnov bnz bov bra bz call clrwdt daw goto nop nop pop push rcall reset retfie retlw return sleep n n n n n n n n n n, s ? ? n ? ? ? ? n s k s ? branch if carry branch if negative branch if not carry branch if not negative branch if not overflow branch if not zero branch if overflow branch unconditionally branch if zero call subroutine 1st word 2nd word clear watchdog timer decimal adjust wreg go to address 1st word 2nd word no operation no operation pop top of return stack (tos) push top of return stack (tos) relative call software device reset return from interrupt enable return with literal in wreg return from subroutine go into standby mode 1 (2) 1 (2) 1 (2) 1 (2) 1 (2) 2 1 (2) 1 (2) 1 (2) 2 1 1 2 1 1 1 1 2 1 2 2 2 1 1110 1110 1110 1110 1110 1110 1110 1101 1110 1110 1111 0000 0000 1110 1111 0000 1111 0000 0000 1101 0000 0000 0000 0000 0000 0010 0110 0011 0111 0101 0001 0100 0nnn 0000 110s kkkk 0000 0000 1111 kkkk 0000 xxxx 0000 0000 1nnn 0000 0000 1100 0000 0000 nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn kkkk kkkk 0000 0000 kkkk kkkk 0000 xxxx 0000 0000 nnnn 1111 0001 kkkk 0001 0000 nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn nnnn kkkk kkkk 0100 0111 kkkk kkkk 0000 xxxx 0110 0101 nnnn 1111 000s kkkk 001s 0011 none none none none none none none none none none to , pd c, dc none none none none none none all gie/gieh, peie/giel none none to , pd 4 table 23-2: pic18fxxx instruction set (continued) mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is ? 1 ? for a pin configured as input and is driven low by an external device, the data will be written back with a ? 0 ?. 2: if this instruction is executed on the tmr0 register (and, where applicable, d = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are 2-word instructions. the second word of these instructions will be executed as a nop , unless the first word of the instruction retrieves the information embedded in these 16 bits. this ensures that all program memory locations have a valid instruction. 5: if the table write starts the write cycle to internal memory, the write will continue until terminated.
pic18f2331/2431/4331/4431 ds39616b-page 292 preliminary ? 2003 microchip technology inc. literal operations addlw andlw iorlw lfsr movlb movlw mullw retlw sublw xorlw k k k f, k k k k k k k add literal and wreg and literal with wreg inclusive or literal with wreg move literal (12-bit) 2nd word to fsrx 1st word move literal to bsr<3:0> move literal to wreg multiply literal with wreg return with literal in wreg subtract wreg from literal exclusive or literal with wreg 1 1 1 2 1 1 1 2 1 1 0000 0000 0000 1110 1111 0000 0000 0000 0000 0000 0000 1111 1011 1001 1110 0000 0001 1110 1101 1100 1000 1010 kkkk kkkk kkkk 00ff kkkk 0000 kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk c, dc, z, ov, n z, n z, n none none none none none c, dc, z, ov, n z, n data memory ? program memory operations tblrd* tblrd*+ tblrd*- tblrd+* tblwt* tblwt*+ tblwt*- tblwt+* table read table read with post-increment table read with post-decrement table read with pre-increment table write table write with post-increment table write with post-decrement table write with pre-increment 2 2 (5) 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1000 1001 1010 1011 1100 1101 1110 1111 none none none none none none none none table 23-2: pic18fxxx instruction set (continued) mnemonic, operands description cycles 16-bit instruction word status affected notes msb lsb note 1: when a port register is modified as a function of itself (e.g., movf portb, 1, 0 ), the value used will be that value present on the pins themselves. for example, if the data latch is ? 1 ? for a pin configured as input and is driven low by an external device, the data will be written back with a ? 0 ?. 2: if this instruction is executed on the tmr0 register (and, where applicable, d = 1 ), the prescaler will be cleared if assigned. 3: if program counter (pc) is modified or a conditional test is true, the instruction requires two cycles. the second cycle is executed as a nop . 4: some instructions are 2-word instructions. the second word of these instructions will be executed as a nop , unless the first word of the instruction retrieves the information embedded in these 16 bits. this ensures that all program memory locations have a valid instruction. 5: if the table write starts the write cycle to internal memory, the write will continue until terminated.
? 2003 microchip technology inc. preliminary ds39616b-page 293 pic18f2331/2431/4331/4431 23.2 instruction set addlw add literal to w syntax: [ label ] addlw k operands: 0 k 255 operation: (w) + k w status affected: n, ov, c, dc, z encoding: 0000 1111 kkkk kkkk description: the contents of w are added to the 8-bit literal ?k? and the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : addlw 0x15 before instruction w = 0x10 after instruction w = 0x25 addwf add w to f syntax: [ label ] addwf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (w) + (f) dest status affected: n, ov, c, dc, z encoding: 0010 01da ffff ffff description: add w to register ?f?. if ?d? is 0 , the result is stored in w. if ?d? is 1 , the result is stored back in register ?f? (default). if ?a? is 0 , the access bank will be selected. if ?a? is 1 , the bsr is used. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : addwf reg, w before instruction w = 0x17 reg = 0xc2 after instruction w=0xd9 reg = 0xc2
pic18f2331/2431/4331/4431 ds39616b-page 294 preliminary ? 2003 microchip technology inc. addwfc add w and carry bit to f syntax: [ label ] addwfc f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (w) + (f) + (c) dest status affected: n, ov, c, dc, z encoding: 0010 00da ffff ffff description: add w, the carry flag and data memory location ?f?. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed in data memory loca- tion ?f?. if ?a? is 0 , the access bank will be selected. if ?a? is 1 , the bsr will not be overridden. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : addwfc reg, w before instruction carry bit = 1 reg = 0x02 w = 0x4d after instruction carry bit = 0 reg = 0x02 w = 0x50 andlw and literal with w syntax: [ label ] andlw k operands: 0 k 255 operation: (w) .and. k w status affected: n, z encoding: 0000 1011 kkkk kkkk description: the contents of w are anded with the 8-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : andlw 0x5f before instruction w=0xa3 after instruction w = 0x03
? 2003 microchip technology inc. preliminary ds39616b-page 295 pic18f2331/2431/4331/4431 andwf and w with f syntax: [ label ] andwf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (w) .and. (f) dest status affected: n, z encoding: 0001 01da ffff ffff description: the contents of w are and?ed with register ?f?. if ?d? is 0 , the result is stored in w. if ?d? is 1 , the result is stored back in register ?f? (default). if ?a? is 0 , the access bank will be selected. if ?a? is 1 , the bsr will not be overridden (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register 'f' process data write to destination example : andwf reg, w before instruction w = 0x17 reg = 0xc2 after instruction w = 0x02 reg = 0xc2 bc branch if carry syntax: [ label ] bc n operands: -128 n 127 operation: if carry bit is ? 1 ? (pc) + 2 + 2n pc status affected: none encoding: 1110 0010 nnnn nnnn description: if the carry bit is 1 , then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bc jump before instruction pc = address (here) after instruction if carry = 1; pc = address (jump) if carry = 0; pc = address (here+2)
pic18f2331/2431/4331/4431 ds39616b-page 296 preliminary ? 2003 microchip technology inc. bcf bit clear f syntax: [ label ] bcf f,b[,a] operands: 0 f 255 0 b 7 a [0,1] operation: 0 f status affected: none encoding: 1001 bbba ffff ffff description: bit ?b? in register ?f? is cleared. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : bcf flag_reg, 7 before instruction flag_reg = 0xc7 after instruction flag_reg = 0x47 bn branch if negative syntax: [ label ] bn n operands: -128 n 127 operation: if negative bit is ? 1 ? (pc) + 2 + 2n pc status affected: none encoding: 1110 0110 nnnn nnnn description: if the negative bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bn jump before instruction pc = address (here) after instruction if negative = 1; pc = address (jump) if negative = 0; pc = address (here+2)
? 2003 microchip technology inc. preliminary ds39616b-page 297 pic18f2331/2431/4331/4431 bnc branch if not carry syntax: [ label ] bnc n operands: -128 n 127 operation: if carry bit is ? 0 ? (pc) + 2 + 2n pc status affected: none encoding: 1110 0011 nnnn nnnn description: if the carry bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnc jump before instruction pc = address (here) after instruction if carry = 0; pc = address (jump) if carry = 1; pc = address (here+2) bnn branch if not negative syntax: [ label ] bnn n operands: -128 n 127 operation: if negative bit is ? 0 ? (pc) + 2 + 2n pc status affected: none encoding: 1110 0111 nnnn nnnn description: if the negative bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnn jump before instruction pc = address (here) after instruction if negative = 0; pc = address (jump) if negative = 1; pc = address (here+2)
pic18f2331/2431/4331/4431 ds39616b-page 298 preliminary ? 2003 microchip technology inc. bnov branch if not overflow syntax: [ label ] bnov n operands: -128 n 127 operation: if overflow bit is ? 0 ? (pc) + 2 + 2n pc status affected: none encoding: 1110 0101 nnnn nnnn description: if the overflow bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnov jump before instruction pc = address (here) after instruction if overflow = 0; pc = address (jump) if overflow = 1; pc = address (here+2) bnz branch if not zero syntax: [ label ] bnz n operands: -128 n 127 operation: if zero bit is ? 0 ? (pc) + 2 + 2n pc status affected: none encoding: 1110 0001 nnnn nnnn description: if the zero bit is ? 0 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bnz jump before instruction pc = address (here) after instruction if zero = 0; pc = address (jump) if zero = 1; pc = address (here+2)
? 2003 microchip technology inc. preliminary ds39616b-page 299 pic18f2331/2431/4331/4431 bra unconditional branch syntax: [ label ] bra n operands: -1024 n 1023 operation: (pc) + 2 + 2n pc status affected: none encoding: 1101 0nnn nnnn nnnn description: add the 2?s complement number ?2n? to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is a two- cycle instruction. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation example : here bra jump before instruction pc = address (here) after instruction pc = address (jump) bsf bit set f syntax: [ label ] bsf f,b[,a] operands: 0 f 255 0 b 7 a [0,1] operation: 1 f status affected: none encoding: 1000 bbba ffff ffff description: bit ?b? in register 'f' is set. if ?a? is 0 , access bank will be selected, over- riding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : bsf flag_reg, 7 before instruction flag_reg = 0x0a after instruction flag_reg = 0x8a
pic18f2331/2431/4331/4431 ds39616b-page 300 preliminary ? 2003 microchip technology inc. btfsc bit test file, skip if clear syntax: [ label ] btfsc f,b[,a] operands: 0 f 255 0 b 7 a [0,1] operation: skip if (f) = 0 status affected: none encoding: 1011 bbba ffff ffff description: if bit ?b? in register ?f? is 0 , then the next instruction is skipped. if bit ?b? is 0 , then the next instruction fetched during the current instruction execution is discarded, and a nop is executed instead, making this a two- cycle instruction. if ?a? is 0 , the access bank will be selected, over- riding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here false true btfsc : : flag, 1 before instruction pc = address (here) after instruction if flag<1> = 0; pc = address (true) if flag<1> = 1; pc = address (false) btfss bit test file, skip if set syntax: [ label ] btfss f,b[,a] operands: 0 f 255 0 b < 7 a [0,1] operation: skip if (f) = 1 status affected: none encoding: 1010 bbba ffff ffff description: if bit ?b? in register ?f? is 1 , then the next instruction is skipped. if bit ?b? is 1 , then the next instruction fetched during the current instruc- tion execution, is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is 0 , the access bank will be selected, over- riding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here false true btfss : : flag, 1 before instruction pc = address (here) after instruction if flag<1> = 0; pc = address (false) if flag<1> = 1; pc = address (true)
? 2003 microchip technology inc. preliminary ds39616b-page 301 pic18f2331/2431/4331/4431 btg bit toggle f syntax: [ label ] btg f,b[,a] operands: 0 f 255 0 b < 7 a [0,1] operation: (f ) f status affected: none encoding: 0111 bbba ffff ffff description: bit ?b? in data memory location ?f? is inverted. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : btg portc, 4 before instruction: portc = 0111 0101 [0x75] after instruction: portc = 0110 0101 [0x65] bov branch if overflow syntax: [ label ] bov n operands: -128 n 127 operation: if overflow bit is ? 1 ? (pc) + 2 + 2n pc status affected: none encoding: 1110 0100 nnnn nnnn description: if the overflow bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bov jump before instruction pc = address (here) after instruction if overflow = 1; pc = address (jump) if overflow = 0; pc = address (here+2)
pic18f2331/2431/4331/4431 ds39616b-page 302 preliminary ? 2003 microchip technology inc. bz branch if zero syntax: [ label ] bz n operands: -128 n 127 operation: if zero bit is ? 1 ? (pc) + 2 + 2n pc status affected: none encoding: 1110 0000 nnnn nnnn description: if the zero bit is ? 1 ?, then the program will branch. the 2?s complement number ?2n? is added to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is then a two-cycle instruction. words: 1 cycles: 1(2) q cycle activity: if jump: q1 q2 q3 q4 decode read literal ?n? process data write to pc no operation no operation no operation no operation if no jump: q1 q2 q3 q4 decode read literal ?n? process data no operation example : here bz jump before instruction pc = address (here) after instruction if zero = 1; pc = address (jump) if zero = 0; pc = address (here+2) call subroutine call syntax: [ label ] call k [,s] operands: 0 k 1048575 s [0,1] operation: (pc) + 4 tos, k pc<20:1>, if s = 1 (w) ws, (status) statuss, (bsr) bsrs status affected: none encoding: 1st word (k<7:0>) 2nd word(k<19:8>) 1110 1111 110s k 19 kkk k 7 kkk kkkk kkkk 0 kkkk 8 description: subroutine call of entire 2 mbyte memory range. first, return address (pc+ 4) is pushed onto the return stack. if ?s? = 1 , the w, status and bsr registers are also pushed into their respective shadow registers, ws, statuss and bsrs. if ?s? = 0 , no update occurs (default). then, the 20-bit value ?k? is loaded into pc<20:1>. call is a two-cycle instruction. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k?<7:0>, push pc to stack read literal ?k?<19:8>, write to pc no operation no operation no operation no operation example : here call there,fast before instruction pc = address (here) after instruction pc = address (there) tos = address (here + 4) ws = w bsrs = bsr statuss= status
? 2003 microchip technology inc. preliminary ds39616b-page 303 pic18f2331/2431/4331/4431 clrf clear f syntax: [ label ] clrf f [,a] operands: 0 f 255 a [0,1] operation: 000h f 1 z status affected: z encoding: 0110 101a ffff ffff description: clears the contents of the specified register. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : clrf flag_reg before instruction flag_reg = 0x5a after instruction flag_reg = 0x00 clrwdt clear watchdog timer syntax: [ label ] clrwdt operands: none operation: 000h wdt, 000h wdt postscaler, 1 to, 1 pd status affected: to , pd encoding: 0000 0000 0000 0100 description: clrwdt instruction resets the watchdog timer. it also resets the postscaler of the wdt. status bits to and pd are set. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation process data no operation example : clrwdt before instruction wdt counter = ? after instruction wdt counter = 0x00 wdt postscaler = 0 to =1 pd =1
pic18f2331/2431/4331/4431 ds39616b-page 304 preliminary ? 2003 microchip technology inc. comf complement f syntax: [ label ] comf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: dest status affected: n, z encoding: 0001 11da ffff ffff description: the contents of register ?f? are com- plemented. if ?d? is 0 , the result is stored in w. if ?d? is 1 , the result is stored back in register ?f? (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : comf reg, w before instruction reg = 0x13 after instruction reg = 0x13 w=0xec (f ) cpfseq compare f with w, skip if f = w syntax: [ label ] cpfseq f [,a] operands: 0 f 255 a [0,1] operation: (f) ? (w), skip if (f) = (w) (unsigned comparison) status affected: none encoding: 0110 001a ffff ffff description: compares the contents of data memory location ?f? to the contents of w by performing an unsigned subtraction. if ?f? = w , then the fetched instruc- tion is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is 0 , the access bank will be selected, over- riding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f'? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfseq reg nequal : equal : before instruction pc address = here w=? reg = ? after instruction if reg = w; pc = address (equal) if reg w; pc = address (nequal)
? 2003 microchip technology inc. preliminary ds39616b-page 305 pic18f2331/2431/4331/4431 cpfsgt compare f with w, skip if f > w syntax: [ label ] cpfsgt f [,a] operands: 0 f 255 a [0,1] operation: (f) ? ( w), skip if (f) > (w) (unsigned comparison) status affected: none encoding: 0110 010a ffff ffff description: compares the contents of data memory location ?f? to the contents of the w by performing an unsigned subtraction. if the contents of ?f? are greater than the contents of wreg , then the fetched instruction is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfsgt reg ngreater : greater : before instruction pc = address (here) w= ? after instruction if reg > w; pc = address (greater) if reg w; pc = address (ngreater) cpfslt compare f with w, skip if f < w syntax: [ label ] cpfslt f [,a] operands: 0 f 255 a [0,1] operation: (f) ? ( w), skip if (f) < (w) (unsigned comparison) status affected: none encoding: 0110 000a ffff ffff description: compares the contents of data memory location ?f? to the contents of w by performing an unsigned subtraction. if the contents of ?f? are less than the contents of w, then the fetched instruction is discarded and a nop is executed instead, making this a two-cycle instruction. if ?a? is 0 , the access bank will be selected. if ?a? is 1 , the bsr will not be overridden (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here cpfslt reg nless : less : before instruction pc = address (here) w= ? after instruction if reg < w; pc = address (less) if reg w; pc = address (nless)
pic18f2331/2431/4331/4431 ds39616b-page 306 preliminary ? 2003 microchip technology inc. daw decimal adjust w register syntax: [ label ] daw operands: none operation: if [w<3:0> >9] or [dc = 1] then (w<3:0>) + 6 w<3:0>; else ( w<3:0>) w<3:0>; if [w<7:4> >9] or [c = 1] then ( w<7:4>) + 6 w<7:4>; else (w<7:4>) w<7:4>; status affected: c, dc encoding: 0000 0000 0000 0111 description: daw adjusts the eight-bit value in w, resulting from the earlier addition of two variables (each in packed bcd format) and produces a correct packed bcd result. the carry bit may be set by daw regardless of its setting prior to the daw instruction. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register w process data write w example1 : daw before instruction w=0xa5 c=0 dc = 0 after instruction w = 0x05 c=1 dc = 0 example 2 : before instruction w=0xce c=0 dc = 0 after instruction w = 0x34 c=1 dc = 0 decf decrement f syntax: [ label ] decf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) ? 1 dest status affected: c, dc, n, ov, z encoding: 0000 01da ffff ffff description: decrement register ?f?. if ?d? is 0 , the result is stored in w. if ?d? is 1 , the result is stored back in register ?f? (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : decf cnt, before instruction cnt = 0x01 z=0 after instruction cnt = 0x00 z=1
? 2003 microchip technology inc. preliminary ds39616b-page 307 pic18f2331/2431/4331/4431 decfsz decrement f, skip if 0 syntax: [ label ] decfsz f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) ? 1 dest, skip if result = 0 status affected: none encoding: 0010 11da ffff ffff description: the contents of register ?f? are dec- remented. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed back in register ?f? (default). if the result is 0 , the next instruc- tion, which is already fetched, is discarded, and a nop is executed instead, making it a two-cycle instruction. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here decfsz cnt goto loop continue before instruction pc = address (here) after instruction cnt = cnt - 1 if cnt = 0; pc = address (continue) if cnt 0; pc = address (here+2) dcfsnz decrement f, skip if not 0 syntax: [ label ] dcfsnz f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) ? 1 dest, skip if result 0 status affected: none encoding: 0100 11da ffff ffff description: the contents of register ?f? are dec- remented. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed back in register ?f? (default). if the result is not 0 , the next instruction, which is already fetched, is discarded, and a nop is executed instead, making it a two- cycle instruction. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here dcfsnz temp zero : nzero : before instruction temp = ? after instruction temp = temp - 1, if temp = 0; pc = address (zero) if temp 0; pc = address (nzero)
pic18f2331/2431/4331/4431 ds39616b-page 308 preliminary ? 2003 microchip technology inc. goto unconditional branch syntax: [ label ] goto k operands: 0 k 1048575 operation: k pc<20:1> status affected: none encoding: 1st word (k<7:0>) 2nd word(k<19:8>) 1110 1111 1111 k 19 kkk k 7 kkk kkkk kkkk 0 kkkk 8 description: goto allows an unconditional branch anywhere within entire 2 mbyte memory range. the 20-bit value ?k? is loaded into pc<20:1>. goto is always a two-cycle instruction. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k?<7:0>, no operation read literal ?k?<19:8>, write to pc no operation no operation no operation no operation example : goto there after instruction pc = address (there) incf increment f syntax: [ label ] incf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) + 1 dest status affected: c, dc, n, ov, z encoding: 0010 10da ffff ffff description: the contents of register ?f? are incremented. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed back in register ?f? (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : incf cnt, before instruction cnt = 0xff z=0 c=? dc = ? after instruction cnt = 0x00 z=1 c=1 dc = 1
? 2003 microchip technology inc. preliminary ds39616b-page 309 pic18f2331/2431/4331/4431 incfsz increment f, skip if 0 syntax: [ label ] incfsz f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) + 1 dest, skip if result = 0 status affected: none encoding: 0011 11da ffff ffff description: the contents of register ?f? are incremented. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed back in register ?f?. (default) if the result is 0 , the next instruc- tion, which is already fetched, is discarded, and a nop is executed instead, making it a two-cycle instruction. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here incfsz cnt nzero : zero : before instruction pc = address (here) after instruction cnt = cnt + 1 if cnt = 0; pc = address (zero) if cnt 0; pc = address (nzero) infsnz increment f, skip if not 0 syntax: [ label ] infsnz f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) + 1 dest, skip if result 0 status affected: none encoding: 0100 10da ffff ffff description: the contents of register ?f? are incremented. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed back in register ?f? (default). if the result is not 0 , the next instruction, which is already fetched, is discarded, and a nop is executed instead, making it a two- cycle instruction. if ?a? is 0 , the access bank will be selected, over- riding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here infsnz reg zero nzero before instruction pc = address (here) after instruction reg = reg + 1 if reg 0; pc = address (nzero) if reg = 0; pc = address (zero)
pic18f2331/2431/4331/4431 ds39616b-page 310 preliminary ? 2003 microchip technology inc. iorlw inclusive or literal with w syntax: [ label ] iorlw k operands: 0 k 255 operation: (w) .or. k w status affected: n, z encoding: 0000 1001 kkkk kkkk description: the contents of w are or?ed with the eight-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : iorlw 0x35 before instruction w = 0x9a after instruction w=0xbf iorwf inclusive or w with f syntax: [ label ] iorwf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (w) .or. (f) dest status affected: n, z encoding: 0001 00da ffff ffff description: inclusive or w with register ?f?. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed back in register ?f? (default). if ?a? is 0 , the access bank will be selected, over- riding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : iorwf result, w before instruction result = 0x13 w = 0x91 after instruction result = 0x13 w = 0x93
? 2003 microchip technology inc. preliminary ds39616b-page 311 pic18f2331/2431/4331/4431 lfsr load fsr syntax: [ label ] lfsr f,k operands: 0 f 2 0 k 4095 operation: k fsrf status affected: none encoding: 1110 1111 1110 0000 00ff k 7 kkk k 11 kkk kkkk description: the 12-bit literal ?k? is loaded into the file select register pointed to by ?f?. words: 2 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k? msb process data write literal ?k? msb to fsrfh decode read literal ?k? lsb process data write literal ?k? to fsrfl example : lfsr 2, 0x3ab after instruction fsr2h = 0x03 fsr2l = 0xab movf move f syntax: [ label ] movf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: f dest status affected: n, z encoding: 0101 00da ffff ffff description: the contents of register ?f? are moved to a destination dependent upon the status of ?d?. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed back in register ?f? (default). location ?f? can be any- where in the 256 byte bank. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write w example : movf reg, w before instruction reg = 0x22 w=0xff after instruction reg = 0x22 w = 0x22
pic18f2331/2431/4331/4431 ds39616b-page 312 preliminary ? 2003 microchip technology inc. movff move f to f syntax: [ label ] movff f s ,f d operands: 0 f s 4095 0 f d 4095 operation: (f s ) f d status affected: none encoding: 1st word (source) 2nd word (destin.) 1100 1111 ffff ffff ffff ffff fff f s fff f d description: the contents of source register ?f s ? are moved to destination register ?f d ?. location of source ?f s ? can be anywhere in the 4096 byte data space (000h to fffh) and location of destination ?f d ? can also be any- where from 000h to fffh. either source or destination can be w (a useful special situation). movff is particularly useful for transferring a data memory location to a peripheral register (such as the transmit buffer or an i/o port). the movff instruction cannot use the pcl, tosu, tosh or tosl as the destination register. the movff instruction should not be used to modify interrupt settings while any interrupt is enabled (see the note on page 91). words: 2 cycles: 2 (3) q cycle activity: q1 q2 q3 q4 decode read register ?f? (src) process data no operation decode no operation no dummy read no operation write register ?f? (dest) example : movff reg1, reg2 before instruction reg1 = 0x33 reg2 = 0x11 after instruction reg1 = 0x33, reg2 = 0x33 movlb move literal to low nibble in bsr syntax: [ label ] movlb k operands: 0 k 255 operation: k bsr status affected: none encoding: 0000 0001 kkkk kkkk description: the 8-bit literal ?k? is loaded into the bank select register (bsr). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write literal ?k? to bsr example : movlb 5 before instruction bsr register = 0x02 after instruction bsr register = 0x05
? 2003 microchip technology inc. preliminary ds39616b-page 313 pic18f2331/2431/4331/4431 movlw move literal to w syntax: [ label ] movlw k operands: 0 k 255 operation: k w status affected: none encoding: 0000 1110 kkkk kkkk description: the eight-bit literal ?k? is loaded into w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : movlw 0x5a after instruction w = 0x5a movwf move w to f syntax: [ label ] movwf f [,a] operands: 0 f 255 a [0,1] operation: (w) f status affected: none encoding: 0110 111a ffff ffff description: move data from w to register ?f?. location ?f? can be anywhere in the 256 byte bank. if ?a? is 0 , the access bank will be selected, over- riding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : movwf reg before instruction w = 0x4f reg = 0xff after instruction w = 0x4f reg = 0x4f
pic18f2331/2431/4331/4431 ds39616b-page 314 preliminary ? 2003 microchip technology inc. mullw multiply literal with w syntax: [ label ] mullw k operands: 0 k 255 operation: (w) x k prodh:prodl status affected: none encoding: 0000 1101 kkkk kkkk description: an unsigned multiplication is carried out between the contents of w and the 8-bit literal ?k?. the 16-bit result is placed in prodh:prodl register pair. prodh contains the high byte. w is unchanged. none of the status flags are affected. note that neither overflow nor carry is possible in this opera- tion. a zero result is possible but not detected. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write registers prodh: prodl example : mullw 0xc4 before instruction w=0xe2 prodh = ? prodl = ? after instruction w=0xe2 prodh = 0xad prodl = 0x08 mulwf multiply w with f syntax: [ label ] mulwf f [,a] operands: 0 f 255 a [0,1] operation: (w) x (f) prodh:prodl status affected: none encoding: 0000 001a ffff ffff description: an unsigned multiplication is car- ried out between the contents of w and the register file location ?f?. the 16-bit result is stored in the prodh:prodl register pair. prodh contains the high byte. both w and ?f? are unchanged. none of the status flags are affected. note that neither overflow nor carry is possible in this opera- tion. a zero result is possible but not detected. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a?= 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write registers prodh: prodl example : mulwf reg before instruction w=0xc4 reg = 0xb5 prodh = ? prodl = ? after instruction w=0xc4 reg = 0xb5 prodh = 0x8a prodl = 0x94
? 2003 microchip technology inc. preliminary ds39616b-page 315 pic18f2331/2431/4331/4431 negf negate f syntax: [ label ] negf f [,a] operands: 0 f 255 a [0,1] operation: ( f ) + 1 f status affected: n, ov, c, dc, z encoding: 0110 110a ffff ffff description: location ?f? is negated using two?s complement. the result is placed in the data memory location ?f?. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : negf reg, 1 before instruction reg = 0011 1010 [0x3a] after instruction reg = 1100 0110 [0xc6] nop no operation syntax: [ label ] nop operands: none operation: no operation status affected: none encoding: 0000 1111 0000 xxxx 0000 xxxx 0000 xxxx description: no operation. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation example : none.
pic18f2331/2431/4331/4431 ds39616b-page 316 preliminary ? 2003 microchip technology inc. pop pop top of return stack syntax: [ label ] pop operands: none operation: (tos) bit bucket status affected: none encoding: 0000 0000 0000 0110 description: the tos value is pulled off the return stack and is discarded. the tos value then becomes the previous value that was pushed onto the return stack. this instruction is provided to enable the user to properly manage the return stack to incorporate a software stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation pop tos value no operation example : pop goto new before instruction tos = 0x0031a2 stack (1 level down) = 0x014332 after instruction tos = 0x014332 pc = new push push top of return stack syntax: [ label ] push operands: none operation: (pc+2) tos status affected: none encoding: 0000 0000 0000 0101 description: the pc+2 is pushed onto the top of the return stack. the previous tos value is pushed down on the stack. this instruction allows to implement a software stack by modifying tos, and then push it onto the return stack. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode push pc+2 onto return stack no operation no operation example : push before instruction tos = 0x00345a pc = 0x000124 after instruction pc = 0x000126 tos = 0x000126 stack (1 level down) = 0x00345a
? 2003 microchip technology inc. preliminary ds39616b-page 317 pic18f2331/2431/4331/4431 rcall relative call syntax: [ label ] rcall n operands: -1024 n 1023 operation: (pc) + 2 tos, (pc) + 2 + 2n pc status affected: none encoding: 1101 1nnn nnnn nnnn description: subroutine call with a jump up to 1k from the current location. first, return address (pc+2) is pushed onto the stack. then, add the 2?s complement number ?2n? to the pc. since the pc will have incremented to fetch the next instruction, the new address will be pc+2+2n. this instruction is a two-cycle instruc- tion. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?n? push pc to stack process data write to pc no operation no operation no operation no operation example : here rcall jump before instruction pc = address (here) after instruction pc = address (jump) tos = address (here+2) reset reset syntax: [ label ] reset operands: none operation: reset all registers and flags that are affected by a mclr reset. status affected: all encoding: 0000 0000 1111 1111 description: this instruction provides a way to execute a mclr reset in software. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode start reset no operation no operation example : reset after instruction registers = reset value flags* = reset value
pic18f2331/2431/4331/4431 ds39616b-page 318 preliminary ? 2003 microchip technology inc. retfie return from interrupt syntax: [ label ] retfie [s] operands: s [0,1] operation: (tos) pc, 1 gie/gieh or peie/giel, if s = 1 (ws) w, (statuss) status, (bsrs) bsr, pclatu, pclath are unchanged. status affected: gie/gieh, peie/giel. encoding: 0000 0000 0001 000s description: return from interrupt. stack is popped and top-of-stack (tos) is loaded into the pc. interrupts are enabled by setting either the high or low priority global interrupt enable bit. if ?s? = 1 , the contents of the shadow registers ws, statuss and bsrs are loaded into their corresponding registers, w, status and bsr. if ?s? = 0 , no update of these registers occurs (default). words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation pop pc from stack set gieh or giel no operation no operation no operation no operation example : retfie 1 after interrupt pc = tos w=ws bsr = bsrs status = statuss gie/gieh, peie/giel = 1 retlw return literal to w syntax: [ label ] retlw k operands: 0 k 255 operation: k w, (tos) pc, pclatu, pclath are unchanged status affected: none encoding: 0000 1100 kkkk kkkk description: w is loaded with the eight-bit literal ?k?. the program counter is loaded from the top of the stack (the return address). the high address latch (pclath) remains unchanged. words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data pop pc from stack, write to w no operation no operation no operation no operation example : call table ; w contains table ; offset value ; w now has ; table value : table addwf pcl ; w = offset retlw k0 ; begin table retlw k1 ; : : retlw kn ; end of table before instruction w = 0x07 after instruction w = value of kn
? 2003 microchip technology inc. preliminary ds39616b-page 319 pic18f2331/2431/4331/4431 return return from subroutine syntax: [ label ] return [s] operands: s [0,1] operation: (tos) pc, if s = 1 (ws) w, (statuss) status, (bsrs) bsr, pclatu, pclath are unchanged status affected: none encoding: 0000 0000 0001 001s description: return from subroutine. the stack is popped and the top of the stack (tos) is loaded into the program counter. if ?s?= 1 , the contents of the shadow registers ws, statuss and bsrs are loaded into their cor- responding registers, w, status and bsr. if ?s? = 0 , no update of these registers occurs (default). words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation process data pop pc from stack no operation no operation no operation no operation example : return after interrupt pc = tos rlcf rotate left f through carry syntax: [ label ] rlcf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) dest, (f<7>) c, (c) dest<0> status affected: c, n, z encoding: 0011 01da ffff ffff description: the contents of register ?f? are rotated one bit to the left through the carry flag. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is stored back in register ?f? (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? = 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : rlcf reg, w before instruction reg = 1110 0110 c= 0 after instruction reg = 1110 0110 w = 1100 1100 c= 1 c register f
pic18f2331/2431/4331/4431 ds39616b-page 320 preliminary ? 2003 microchip technology inc. rlncf rotate left f (no carry) syntax: [ label ] rlncf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) dest, (f<7>) dest<0> status affected: n, z encoding: 0100 01da ffff ffff description: the contents of register ?f? are rotated one bit to the left. if ?d? is 0 , the result is placed in w. if ?d? is 1, the result is stored back in register ?f? (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? is 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : rlncf reg before instruction reg = 1010 1011 after instruction reg = 0101 0111 register f rrcf rotate right f through carry syntax: [ label ] rrcf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) dest, (f<0>) c, (c) dest<7> status affected: c, n, z encoding: 0011 00da ffff ffff description: the contents of register ?f? are rotated one bit to the right through the carry flag. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed back in register ?f? (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? is 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : rrcf reg, w before instruction reg = 1110 0110 c= 0 after instruction reg = 1110 0110 w = 0111 0011 c= 0 c register f
? 2003 microchip technology inc. preliminary ds39616b-page 321 pic18f2331/2431/4331/4431 rrncf rotate right f (no carry) syntax: [ label ] rrncf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) dest, (f<0>) dest<7> status affected: n, z encoding: 0100 00da ffff ffff description: the contents of register ?f? are rotated one bit to the right. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed back in regis- ter ?f? (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? is 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : rrncf reg, 1, 0 before instruction reg = 1101 0111 after instruction reg = 1110 1011 example 2 : rrncf reg, w before instruction w=? reg = 1101 0111 after instruction w = 1110 1011 reg = 1101 0111 register f setf set f syntax: [ label ] setf f [,a] operands: 0 f 255 a [0,1] operation: ffh f status affected: none encoding: 0110 100a ffff ffff description: the contents of the specified register are set to ffh. if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? is 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write register ?f? example : setf reg before instruction reg = 0x5a after instruction reg = 0xff
pic18f2331/2431/4331/4431 ds39616b-page 322 preliminary ? 2003 microchip technology inc. sleep enter sleep mode syntax: [ label ] sleep operands: none operation: 00h wdt, 0 wdt postscaler, 1 to , 0 pd status affected: to , pd encoding: 0000 0000 0000 0011 description: the power-down status bit (pd ) is cleared. the time-out status bit (to ) is set. watchdog timer and its postscaler are cleared. the processor is put into sleep mode with the oscillator stopped. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode no operation process data go to sleep example : sleep before instruction to =? pd =? after instruction to =1 ? pd =0 ? if wdt causes wake-up, this bit is cleared. subfwb subtract f from w with borrow syntax: [ label ] subfwb f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (w) ? (f) ? (c ) dest status affected: n, ov, c, dc, z encoding: 0101 01da ffff ffff description: subtract register ?f? and carry flag (borrow) from w (2?s complement method). if ?d? is 0 , the result is stored in w. if ?d? is 1 , the result is stored in register ?f? (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? is 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : subfwb reg before instruction reg = 0x03 w = 0x02 c = 0x01 after instruction reg = 0xff w = 0x02 c = 0x00 z = 0x00 n = 0x01 ; result is negative example 2 : subfwb reg, 0, 0 before instruction reg = 2 w=5 c=1 after instruction reg = 2 w=3 c=1 z=0 n = 0 ; result is positive example 3 : subfwb reg, 1, 0 before instruction reg = 1 w=2 c=0 after instruction reg = 0 w=2 c=1 z = 1 ; result is zero n=0
? 2003 microchip technology inc. preliminary ds39616b-page 323 pic18f2331/2431/4331/4431 sublw subtract w from literal syntax: [ label ]sublw k operands: 0 k 255 operation: k ? (w) w status affected: n, ov, c, dc, z encoding: 0000 1000 kkkk kkkk description: w is subtracted from the eight-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example 1: sublw 0x02 before instruction w=1 c=? after instruction w=1 c = 1 ; result is positive z=0 n=0 example 2 : sublw 0x02 before instruction w=2 c=? after instruction w=0 c = 1 ; result is zero z=1 n=0 example 3 : sublw 0x02 before instruction w=3 c=? after instruction w = ff ; (2?s complement) c = 0 ; result is negative z=0 n=1 subwf subtract w from f syntax: [ label ] subwf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) ? (w) dest status affected: n, ov, c, dc, z encoding: 0101 11da ffff ffff description: subtract w from register ?f? (2?s complement method). if ?d? is 0 , the result is stored in w. if ?d? is 1 , the result is stored back in regis- ter ?f? (default). if = ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? is 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : subwf reg before instruction reg = 3 w=2 c=? after instruction reg = 1 w=2 c = 1 ; result is positive z=0 n=0 example 2 : subwf reg, w before instruction reg = 2 w=2 c=? after instruction reg = 2 w=0 c = 1 ; result is zero z=1 n=0 example 3 : subwf reg before instruction reg = 0x01 w = 0x02 c=? after instruction reg = 0xffh ;(2?s complement) w = 0x02 c = 0x00 ; result is negative z = 0x00 n = 0x01
pic18f2331/2431/4331/4431 ds39616b-page 324 preliminary ? 2003 microchip technology inc. subwfb subtract w from f with borrow syntax: [ label ] subwfb f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f) ? (w) ? (c ) dest status affected: n, ov, c, dc, z encoding: 0101 10da ffff ffff description: subtract w and the carry flag (bor- row) from register ?f? (2?s complement method). if ?d? is 0 , the result is stored in w. if ?d? is 1 , the result is stored back in register ?f? (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? is 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example 1 : subwfb reg, 1, 0 before instruction reg = 0x19 (0001 1001) w = 0x0d (0000 1101) c = 0x01 after instruction reg = 0x0c (0000 1011) w = 0x0d (0000 1101) c = 0x01 z = 0x00 n = 0x00 ; result is positive example 2 : subwfb reg, 0, 0 before instruction reg = 0x1b (0001 1011) w = 0x1a (0001 1010) c = 0x00 after instruction reg = 0x1b (0001 1011) w = 0x00 c = 0x01 z = 0x01 ; result is zero n = 0x00 example 3: subwfb reg, 1, 0 before instruction reg = 0x03 (0000 0011) w = 0x0e (0000 1101) c = 0x01 after instruction reg = 0xf5 (1111 0100) ; [2?s comp] w = 0x0e (0000 1101) c = 0x00 z = 0x00 n = 0x01 ; result is negative
? 2003 microchip technology inc. preliminary ds39616b-page 325 pic18f2331/2431/4331/4431 swapf swap f syntax: [ label ] swapf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (f<3:0>) dest<7:4>, (f<7:4>) dest<3:0> status affected: none encoding: 0011 10da ffff ffff description: the upper and lower nibbles of reg- ister ?f? are exchanged. if ?d? is 0 , the result is placed in w. if ?d? is 1 , the result is placed in register ?f? (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? is 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : swapf reg before instruction reg = 0x53 after instruction reg = 0x35
pic18f2331/2431/4331/4431 ds39616b-page 326 preliminary ? 2003 microchip technology inc. tblrd table read syntax: [ label ] tblrd ( *; *+; *-; +*) operands: none operation: if tblrd *, (prog mem (tblptr)) tablat; tblptr - no change; if tblrd *+, (prog mem (tblptr)) tablat; (tblptr) +1 tblptr; if tblrd *-, (prog mem (tblptr)) tablat; (tblptr) -1 tblptr; if tblrd +*, (tblptr) +1 tblptr; (prog mem (tblptr)) tablat; status affected:none encoding: 0000 0000 0000 10nn nn=0 * =1 *+ =2 *- =3 +* description: this instruction is used to read the contents of program memory (p.m.). to address the program memory, a pointer called table pointer (tblptr) is used. the tblptr (a 21-bit pointer) points to each byte in the program memory. tblptr has a 2 mbyte address range. tblptr[0] = 0:least significant byte of program memory word tblptr[0] = 1:most significant byte of program memory word the tblrd instruction can modify the value of tblptr as follows:  no change  post-increment  post-decrement  pre-increment words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation no operation no operation (read program memory) no operation no operation (write tablat) tblrd table read (cont?d) example1 : tblrd *+ ; before instruction tablat = 0x55 tblptr = 0x00a356 memory(0x00a356) = 0x34 after instruction tablat = 0x34 tblptr = 0x00a357 example2 : tblrd +* ; before instruction tablat = 0xaa tblptr = 0x01a357 memory(0x01a357) = 0x12 memory(0x01a358) = 0x34 after instruction tablat = 0x34 tblptr = 0x01a358
? 2003 microchip technology inc. preliminary ds39616b-page 327 pic18f2331/2431/4331/4431 tblwt table write syntax: [ label ] tblwt ( *; *+; *-; +*) operands: none operation: if tblwt*, (tablat) holding register; tblptr - no change; if tblwt*+, (tablat) holding register; (tblptr) +1 tblptr; if tblwt*-, (tablat) holding register; (tblptr) -1 tblptr; if tblwt+*, (tblptr) +1 tblptr; (tablat) holding register; status affected: none encoding: 0000 0000 0000 11nn nn=0 * =1 *+ =2 *- =3 +* description: this instruction uses the 3 lsbs of tblptr to determine which of the 8 holding registers the tablat is written to. the holding registers are used to program the contents of program memory (p.m.). (refer to section 6.0 ?flash program memory? for addi- tional details on programming flash memory.) the tblptr (a 21-bit pointer) points to each byte in the program memory. tblptr has a 2 mbtye address range. the lsb of the tblptr selects which byte of the program memory location to access. tblptr[0] = 0 :least significant byte of program memory word tblptr[0] = 1 :most significant byte of program memory word the tblwt instruction can modify the value of tblptr as follows:  no change  post-increment  post-decrement  pre-increment tblwt table write (continued) words: 1 cycles: 2 q cycle activity: q1 q2 q3 q4 decode no operation no operation no operation no operation no operation (read tablat) no operation no operation (write to holding register ) example1 : tblwt *+; before instruction tablat = 0x55 tblptr = 0x00a356 holding register (0x00a356) = 0xff after instructions (table write completion) tablat = 0x55 tblptr = 0x00a357 holding register (0x00a356) = 0x55 example 2 : tblwt +*; before instruction tablat = 0x34 tblptr = 0x01389a holding register (0x01389a) = 0xff holding register (0x01389b) = 0xff after instruction (table write completion) tablat = 0x34 tblptr = 0x01389b holding register (0x01389a) = 0xff holding register (0x01389b) = 0x34
pic18f2331/2431/4331/4431 ds39616b-page 328 preliminary ? 2003 microchip technology inc. tstfsz test f, skip if 0 syntax: [ label ] tstfsz f [,a] operands: 0 f 255 a [0,1] operation: skip if f = 0 status affected: none encoding: 0110 011a ffff ffff description: if ?f? = 0 , the next instruction, fetched during the current instruc- tion execution, is discarded and a nop is executed, making this a two- cycle instruction. if ?a? is 0 , the access bank will be selected, over- riding the bsr value. if ?a? is 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1(2) note: 3 cycles if skip and followed by a 2-word instruction. q cycle activity: q1 q2 q3 q4 decode read register ?f? process data no operation if skip: q1 q2 q3 q4 no operation no operation no operation no operation if skip and followed by 2-word instruction: q1 q2 q3 q4 no operation no operation no operation no operation no operation no operation no operation no operation example : here tstfsz cnt nzero : zero : before instruction pc = address (here) after instruction if cnt = 0x00, pc = address (zero) if cnt 0x00, pc = address (nzero) xorlw exclusive or literal with w syntax: [ label ] xorlw k operands: 0 k 255 operation: (w) .xor. k w status affected: n, z encoding: 0000 1010 kkkk kkkk description: the contents of w are xored with the 8-bit literal ?k?. the result is placed in w. words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read literal ?k? process data write to w example : xorlw 0xaf before instruction w=0xb5 after instruction w = 0x1a
? 2003 microchip technology inc. preliminary ds39616b-page 329 pic18f2331/2431/4331/4431 xorwf exclusive or w with f syntax: [ label ] xorwf f [,d [,a]] operands: 0 f 255 d [0,1] a [0,1] operation: (w) .xor. (f) dest status affected: n, z encoding: 0001 10da ffff ffff description: exclusive or the contents of w with register ?f?. if ?d? is 0 , the result is stored in w. if ?d? is 1 , the result is stored back in the register ?f' (default). if ?a? is 0 , the access bank will be selected, overriding the bsr value. if ?a? is 1 , then the bank will be selected as per the bsr value (default). words: 1 cycles: 1 q cycle activity: q1 q2 q3 q4 decode read register ?f? process data write to destination example : xorwf reg before instruction reg = 0xaf w=0xb5 after instruction reg = 0x1a w=0xb5
pic18f2331/2431/4331/4431 ds39616b-page 330 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page331 pic18f2331/2431/4331/4431 24.0 development support the picmicro ? microcontrollers are supported with a full range of hardware and software development tools:  integrated development environment - mplab ? ide software  assemblers/compilers/linkers - mpasm tm assembler - mplab c17 and mplab c18 c compilers -mplink tm object linker/ mplib tm object librarian - mplab c30 c compiler - mplab asm30 assembler/linker/library  simulators - mplab sim software simulator - mplab dspic30 software simulator emulators - mplab ice 2000 in-circuit emulator - mplab ice 4000 in-circuit emulator  in-circuit debugger - mplab icd 2  device programmers -pro mate ? ii universal device programmer - picstart ? plus development programmer  low cost demonstration boards - picdem tm 1 demonstration board - picdem.net tm demonstration board - picdem 2 plus demonstration board - picdem 3 demonstration board - picdem 4 demonstration board - picdem 17 demonstration board - picdem 18r demonstration board - picdem lin demonstration board - picdem usb demonstration board  evaluation kits -k ee l oq ? - picdem msc -microid ? -can - powersmart ? -analog 24.1 mplab integrated development environment software the mplab ide software brings an ease of software development previously unseen in the 8/16-bit micro- controller market. the mplab ide is a windows ? based application that contains:  an interface to debugging tools - simulator - programmer (sold separately) - emulator (sold separately) - in-circuit debugger (sold separately)  a full-featured editor with color coded context  a multiple project manager  customizable data windows with direct edit of contents  high level source code debugging  mouse over variable inspection  extensive on-line help the mplab ide allows you to:  edit your source files (either assembly or c)  one touch assemble (or compile) and download to picmicro emulator and simulator tools (automatically updates all project information)  debug using: - source files (assembly or c) - absolute listing file (mixed assembly and c) - machine code mplab ide supports multiple debugging tools in a single development paradigm, from the cost effective simulators, through low cost in-circuit debuggers, to full-featured emulators. this eliminates the learning curve when upgrading to tools with increasing flexibility and power. 24.2 mpasm assembler the mpasm assembler is a full-featured, universal macro assembler for all picmicro mcus. the mpasm assembler generates relocatable object files for the mplink object linker, intel ? standard hex files, map files to detail memory usage and symbol ref- erence, absolute lst files that contain source lines and generated machine code and coff files for debugging. the mpasm assembler features include:  integration into mplab ide projects  user defined macros to streamline assembly code  conditional assembly for multi-purpose source files  directives that allow complete control over the assembly process
pic18f2331/2431/4331/4431 ds39616b-page 332 preliminary ? 2003 microchip technology inc. 24.3 mplab c17 and mplab c18 c compilers the mplab c17 and mplab c18 code development systems are complete ansi c compilers for microchip?s pic17cxxx and pic18cxxx family of microcontrollers. these compilers provide powerful integration capabilities, superior code optimization and ease of use not found with other compilers. for easy source level debugging, the compilers provide symbol information that is optimized to the mplab ide debugger. 24.4 mplink object linker/ mplib object librarian the mplink object linker combines relocatable objects created by the mpasm assembler and the mplab c17 and mplab c18 c compilers. it can link relocatable objects from pre-compiled libraries, using directives from a linker script. the mplib object librarian manages the creation and modification of library files of pre-compiled code. when a routine from a library is called from a source file, only the modules that contain that routine will be linked in with the application. this allows large libraries to be used efficiently in many different applications. the object linker/library features include:  efficient linking of single libraries instead of many smaller files  enhanced code maintainability by grouping related modules together  flexible creation of libraries with easy module listing, replacement, deletion and extraction 24.5 mplab c30 c compiler the mplab c30 c compiler is a full-featured, ansi compliant, optimizing compiler that translates standard ansi c programs into dspic30f assembly language source. the compiler also supports many command- line options and language extensions to take full advantage of the dspic30f device hardware capabili- ties, and afford fine control of the compiler code generator. mplab c30 is distributed with a complete ansi c standard library. all library functions have been vali- dated and conform to the ansi c library standard. the library includes functions for string manipulation, dynamic memory allocation, data conversion, time- keeping, and math functions (trigonometric, exponen- tial and hyperbolic). the compiler provides symbolic information for high level source debugging with the mplab ide. 24.6 mplab asm30 assembler, linker, and librarian mplab asm30 assembler produces relocatable machine code from symbolic assembly language for dspic30f devices. mplab c30 compiler uses the assembler to produce it?s object file. the assembler generates relocatable object files that can then be archived or linked with other relocatable object files and archives to create an executable file. notable features of the assembler include:  support for the entire dspic30f instruction set  support for fixed-point and floating-point data  command line interface  rich directive set  flexible macro language  mplab ide compatibility 24.7 mplab sim software simulator the mplab sim software simulator allows code devel- opment in a pc hosted environment by simulating the picmicro series microcontrollers on an instruction level. on any given instruction, the data areas can be examined or modified and stimuli can be applied from a file, or user defined key press, to any pin. the execu- tion can be performed in single-step, execute-until- break or trace mode. the mplab sim simulator fully supports symbolic debugging using the mplab c17 and mplab c18 c compilers, as well as the mpasm assembler. the software simulator offers the flexibility to develop and debug code outside of the laboratory environment, making it an excellent, economical software development tool. 24.8 mplab sim30 software simulator the mplab sim30 software simulator allows code development in a pc hosted environment by simulating the dspic30f series microcontrollers on an instruction level. on any given instruction, the data areas can be examined or modified and stimuli can be applied from a file, or user defined key press, to any of the pins. the mplab sim30 simulator fully supports symbolic debugging using the mplab c30 c compiler and mplab asm30 assembler. the simulator runs in either a command line mode for automated tasks, or from mplab ide. this high-speed simulator is designed to debug, analyze and optimize time intensive dsp routines.
? 2003 microchip technology inc. preliminary ds39616b-page333 pic18f2331/2431/4331/4431 24.9 mplab ice 2000 high performance universal in-circuit emulator the mplab ice 2000 universal in-circuit emulator is intended to provide the product development engineer with a complete microcontroller design tool set for picmicro microcontrollers. software control of the mplab ice 2000 in-circuit emulator is advanced by the mplab integrated development environment, which allows editing, building, downloading and source debugging from a single environment. the mplab ice 2000 is a full-featured emulator sys- tem with enhanced trace, trigger and data monitoring features. interchangeable processor modules allow the system to be easily reconfigured for emulation of differ- ent processors. the universal architecture of the mplab ice in-circuit emulator allows expansion to support new picmicro microcontrollers. the mplab ice 2000 in-circuit emulator system has been designed as a real-time emulation system with advanced features that are typically found on more expensive development tools. the pc platform and microsoft ? windows 32-bit operating system were chosen to best make these features available in a simple, unified application. 24.10 mplab ice 4000 high performance universal in-circuit emulator the mplab ice 4000 universal in-circuit emulator is intended to provide the product development engineer with a complete microcontroller design tool set for high- end picmicro microcontrollers. software control of the mplab ice in-circuit emulator is provided by the mplab integrated development environment, which allows editing, building, downloading and source debugging from a single environment. the mplab icd 4000 is a premium emulator system, providing the features of mplab ice 2000, but with increased emulation memory and high speed perfor- mance for dspic30f and pic18xxxx devices. its advanced emulator features include complex triggering and timing, up to 2 mb of emulation memory, and the ability to view variables in real-time. the mplab ice 4000 in-circuit emulator system has been designed as a real-time emulation system with advanced features that are typically found on more expensive development tools. the pc platform and microsoft windows 32-bit operating system were cho- sen to best make these features available in a simple, unified application. 24.11 mplab icd 2 in-circuit debugger microchip?s in-circuit debugger, mplab icd 2, is a powerful, low cost, run-time development tool, connecting to the host pc via an rs-232 or high speed usb interface. this tool is based on the flash picmicro mcus and can be used to develop for these and other picmicro microcontrollers. the mplab icd 2 utilizes the in-circuit debugging capability built into the flash devices. this feature, along with microchip?s in-circuit serial programming tm (icsp tm ) protocol, offers cost effective in-circuit flash debugging from the graphical user interface of the mplab inte- grated development environment. this enables a designer to develop and debug source code by setting breakpoints, single-stepping and watching variables, cpu status and peripheral registers. running at full speed enables testing hardware and applications in real-time. mplab icd 2 also serves as a development programmer for selected picmicro devices. 24.12 pro mate ii universal device programmer the pro mate ii is a universal, ce compliant device programmer with programmable voltage verification at v ddmin and v ddmax for maximum reliability. it features an lcd display for instructions and error messages and a modular detachable socket assembly to support various package types. in stand-alone mode, the pro mate ii device programmer can read, verify, and program picmicro devices without a pc connection. it can also set code protection in this mode. 24.13 picstart plus development programmer the picstart plus development programmer is an easy-to-use, low cost, prototype programmer. it con- nects to the pc via a com (rs-232) port. mplab integrated development environment software makes using the programmer simple and efficient. the picstart plus development programmer supports most picmicro devices up to 40 pins. larger pin count devices, such as the pic16c92x and pic17c76x, may be supported with an adapter socket. the picstart plus development programmer is ce compliant.
pic18f2331/2431/4331/4431 ds39616b-page 334 preliminary ? 2003 microchip technology inc. 24.14 picdem 1 picmicro demonstration board the picdem 1 demonstration board demonstrates the capabilities of the pic16c5x (pic16c54 to pic16c58a), pic16c61, pic16c62x, pic16c71, pic16c8x, pic17c42, pic17c43 and pic17c44. all necessary hardware and software is included to run basic demo programs. the sample microcontrollers provided with the picdem 1 demonstration board can be programmed with a pro mate ii device program- mer, or a picstart plus development programmer. the picdem 1 demonstration board can be connected to the mplab ice in-circuit emulator for testing. a pro- totype area extends the circuitry for additional applica- tion components. features include an rs-232 interface, a potentiometer for simulated analog input, push button switches and eight leds. 24.15 picdem.net internet/ethernet demonstration board the picdem.net demonstration board is an internet/ ethernet demonstration board using the pic18f452 microcontroller and tcp/ip firmware. the board supports any 40-pin dip device that conforms to the standard pinout used by the pic16f877 or pic18c452. this kit features a user friendly tcp/ip stack, web server with html, a 24l256 serial eeprom for xmodem download to web pages into serial eeprom, icsp/mplab icd 2 interface con- nector, an ethernet interface, rs-232 interface, and a 16 x 2 lcd display. also included is the book and cd-rom ?tcp/ip lean, web servers for embedded systems,? by jeremy bentham 24.16 picdem 2 plus demonstration board the picdem 2 plus demonstration board supports many 18-, 28-, and 40-pin microcontrollers, including pic16f87x and pic18fxx2 devices. all the neces- sary hardware and software is included to run the dem- onstration programs. the sample microcontrollers provided with the picdem 2 demonstration board can be programmed with a pro mate ii device program- mer, picstart plus development programmer, or mplab icd 2 with a universal programmer adapter. the mplab icd 2 and mplab ice in-circuit emulators may also be used with the picdem 2 demonstration board to test firmware. a prototype area extends the circuitry for additional application components. some of the features include an rs-232 interface, a 2 x 16 lcd display, a piezo speaker, an on-board temperature sensor, four leds, and sample pic18f452 and pic16f877 flash microcontrollers. 24.17 picdem 3 pic16c92x demonstration board the picdem 3 demonstration board supports the pic16c923 and pic16c924 in the plcc package. all the necessary hardware and software is included to run the demonstration programs. 24.18 picdem 4 8/14/18-pin demonstration board the picdem 4 can be used to demonstrate the capa- bilities of the 8-, 14-, and 18-pin pic16xxxx and pic18xxxx mcus, including the pic16f818/819, pic16f87/88, pic16f62xa and the pic18f1320 fam- ily of microcontrollers. picdem 4 is intended to show- case the many features of these low pin count parts, including lin and motor control using eccp. special provisions are made for low-power operation with the supercapacitor circuit, and jumpers allow on-board hardware to be disabled to eliminate current draw in this mode. included on the demo board are provisions for crystal, rc or canned oscillator modes, a five volt regulator for use with a nine volt wall adapter or battery, db-9 rs-232 interface, icd connector for program- ming via icsp and development with mplab icd 2, 2x16 liquid crystal display, pcb footprints for h-bridge motor driver, lin transceiver and eeprom. also included are: header for expansion, eight leds, four potentiometers, three push buttons and a prototyping area. included with the kit is a pic16f627a and a pic18f1320. tutorial firmware is included along with the user?s guide. 24.19 picdem 17 demonstration board the picdem 17 demonstration board is an evaluation board that demonstrates the capabilities of several microchip microcontrollers, including pic17c752, pic17c756a, pic17c762 and pic17c766. a pro- grammed sample is included. the pro mate ii device programmer, or the picstart plus development pro- grammer, can be used to reprogram the device for user tailored application development. the picdem 17 demonstration board supports program download and execution from external on-board flash memory. a generous prototype area is available for user hardware expansion.
? 2003 microchip technology inc. preliminary ds39616b-page335 pic18f2331/2431/4331/4431 24.20 picdem 18r pic18c601/801 demonstration board the picdem 18r demonstration board serves to assist development of the pic18c601/801 family of microchip microcontrollers. it provides hardware implementation of both 8-bit multiplexed/de-multiplexed and 16-bit memory modes. the board includes 2 mb external flash memory and 128 kb sram memory, as well as serial eeprom, allowing access to the wide range of memory types supported by the pic18c601/801. 24.21 picdem lin pic16c43x demonstration board the powerful lin hardware and software kit includes a series of boards and three picmicro microcontrollers. the small footprint pic16c432 and pic16c433 are used as slaves in the lin communication and feature on-board lin transceivers. a pic16f874 flash micro- controller serves as the master. all three microcontrol- lers are programmed with firmware to provide lin bus communication. 24.22 pickit tm 1 flash starter kit a complete "development system in a box", the pickit flash starter kit includes a convenient multi-section board for programming, evaluation, and development of 8/14-pin flash pic ? microcontrollers. powered via usb, the board operates under a simple windows gui. the pickit 1 starter kit includes the user's guide (on cd rom), pickit 1 tutorial software and code for vari- ous applications. also included are mplab ? ide (inte- grated development environment) software, software and hardware "tips 'n tricks for 8-pin flash pic ? microcontrollers" handbook and a usb interface cable. supports all current 8/14-pin flash pic microcontrollers, as well as many future planned devices. 24.23 picdem usb pic16c7x5 demonstration board the picdem usb demonstration board shows off the capabilities of the pic16c745 and pic16c765 usb microcontrollers. this board provides the basis for future usb products. 24.24 evaluation and programming tools in addition to the picdem series of circuits, microchip has a line of evaluation kits and demonstration software for these products. k ee l oq evaluation and programming tools for microchip?s hcs secure data products  can developers kit for automotive network applications  analog design boards and filter design software  powersmart battery charging evaluation/ calibration kits irda ? development kit  microid development and rflab tm development software  seeval ? designer kit for memory evaluation and endurance calculations  picdem msc demo boards for switching mode power supply, high power ir driver, delta sigma adc, and flow rate sensor check the microchip web page and the latest product line card for the complete list of demonstration and evaluation kits.
pic18f2331/2431/4331/4431 ds39616b-page 336 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 337 pic18f2331/2431/4331/4431 25.0 electrical characteristics absolute maximum ratings (?) ambient temperature under bias................................................................................................. ............-55c to +125c storage temperature ............................................................................................................ .................. -65c to +150c voltage on any pin with respect to v ss (except v dd , mclr , and ra4) ......................................... -0.3v to (v dd + 0.3v) voltage on v dd with respect to v ss ......................................................................................................... -0.3v to +7.5v voltage on mclr with respect to v ss (note 2) ......................................................................................... 0v to +13.25v voltage on ra4 with respect to vss ............................................................................................. .................. 0v to +8.5v total power dissipation (note 1) ............................................................................................................................... 1.0w maximum current out of v ss pin ........................................................................................................................... 300 ma maximum current into v dd pin ........................................................................................................................... ...250 ma input clamp current, i ik (v i < 0 or v i > v dd ) ...................................................................................................................... 20 ma output clamp current, i ok (v o < 0 or v o > v dd ) .............................................................................................................. 20 ma maximum output current sunk by any i/o pin..................................................................................... .....................25 ma maximum output current sourced by any i/o pin .................................................................................. ..................25 ma maximum current sunk by all ports ...................................................................................................................... .200 ma maximum current sourced by all ports ........................................................................................... .......................200 ma note 1: power dissipation is calculated as follows: pdis = v dd x {i dd - i oh } + {(v dd -v oh ) x i oh } + (v o l x i ol ) 2: voltage spikes below v ss at the mclr /v pp pin, inducing currents greater than 80 ma, may cause latch-up. thus, a series resistor of 50-100 ? should be used when applying a ?low? level to the mclr /v pp pin, rather than pulling this pin directly to v ss . ? notice: stresses above those listed under ?absolute maximum ratings? may cause permanent damage to the device. this is a stress rating only and functional operation of the device at those or any other conditions above those indicated in the operation listings of this specification is not implied. exposure to maximum rating conditions for extended periods may affect device reliability.
pic18f2331/2431/4331/4431 ds39616b-page 338 preliminary ? 2003 microchip technology inc. figure 25-1: pic18f2331/2431/4331/4431 voltage-frequency graph (industrial) figure 25-2: pic18lf2331/2431/4331/4431 voltage-frequency graph (industrial) frequency voltage 6.0v 5.5v 4.5v 4.0v 2.0v 40 mhz 5.0v 3.5v 3.0v 2.5v pic18f2x31/4x31 4.2v frequency voltage 6.0v 5.5v 4.5v 4.0v 2.0v 40 mhz 5.0v 3.5v 3.0v 2.5v pic18lf2x31/4x31 f max = (16.36 mhz/v) (v ddappmin ? 2.0v) + 4 mhz note: v ddappmin is the minimum voltage of the picmicro ? device in the application. 4 mhz 4.2v
? 2003 microchip technology inc. preliminary ds39616b-page 339 pic18f2331/2431/4331/4431 25.1 dc characteristics: supply voltage pic18f2331/2431/4331/4431 (industrial, extended) pic18lf2331/2431/4331/4431 (industrial) pic18f2331/2431/4331/4431 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf2331/2431/4331/4431 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. symbol characteristic min typ max units conditions v dd supply voltage d001 pic18lf2x31/4x31 2.0 ? 5.5 v hs, xt, rc and lp osc mode pic18f2x31/4x31 4.2 ? 5.5 v d002 v dr ram data retention voltage (1) 1.5 ? ? v d003 v por v dd start voltage to ensure internal power- on reset signal ? ? 0.7 v see section on power-on reset for details d004 s vdd v dd rise rate to ensure internal power- on reset signal 0.05 ? ? v/ms see section on power-on reset for details v bor brown-out reset voltage d005 borv1:borv0 = 10 2.45 ? 2.99 v borv1:borv0 = 01 3.80 ? 4.64 v borv1:borv0 = 00 4.09 ? 4.99 v legend: shading of rows is to assist in readability of the table. note 1: this is the limit to which v dd can be lowered in sleep mode, or duri ng a device reset, without losing ram data.
pic18f2331/2431/4331/4431 ds39616b-page 340 preliminary ? 2003 microchip technology inc. 25.2 dc characteristics: power-down and supply current pic18f2331/2431/4331/4431 (industrial, extended) pic18lf2331/2431/4331/4431 (industrial) pic18f2331/2431/4331/4431 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf2331/2431/4331/4431 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. device typ max units conditions power-down current (i pd ) (1) pic18lf2x31/4x31 0.1 0.5 a -40c v dd = 2.0v, ( sleep mode) 0.1 0.5 a 25c 0.2 1.9 a 85c pic18lf2x31/4x31 0.1 0.5 a -40c v dd = 3.0v, ( sleep mode) 0.1 0.5 a 25c 0.3 1.9 a 85c all devices 0.1 2.0 a -40c v dd = 5.0v, ( sleep mode) 0.1 2.0 a 25c 0.4 6.5 a 85c legend: shading of rows is to assist in readability of the table. note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-ra il; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt enabled/disabled as specified. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be estimated by the formula ir = v dd /2r ext (ma) with r ext in k ? . 4: standard low cost 32 khz crystals have an operating temper ature range of -10c to +70c. extended temperature crystals are available at a much higher cost.
? 2003 microchip technology inc. preliminary ds39616b-page 341 pic18f2331/2431/4331/4431 supply current (i dd ) (2,3) pic18lf2x31/4x31 8 40 a -40c f osc = 31 khz ( rc_run mode, internal oscillator source) 940 a 25c v dd = 2.0v 11 40 a 85c pic18lf2x31/4x31 25 68 a -40c 25 68 a 25c v dd = 3.0v 20 68 a 85c all devices 55 180 a -40c 55 180 a 25c v dd = 5.0v 50 180 a 85c pic18lf2x31/4x31 140 220 a -40c f osc = 1 mhz ( rc_run mode, internal oscillator source) 145 220 a 25c v dd = 2.0v 155 220 a 85c pic18lf2x31/4x31 215 330 a -40c 225 330 a 25c v dd = 3.0v 235 330 a 85c all devices 385 550 a -40c 390 550 a 25c v dd = 5.0v 405 550 a 85c 25.2 dc characteristics: power-down and supply current pic18f2331/2431/4331/4431 (industrial, extended) pic18lf2331/2431/4331/4431 (industrial) (continued) pic18f2331/2431/4331/4431 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf2331/2431/4331/4431 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. device typ max units conditions legend: shading of rows is to assist in readability of the table. note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-ra il; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt enabled/disabled as specified. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be estimated by the formula ir = v dd /2r ext (ma) with r ext in k ? . 4: standard low cost 32 khz crystals have an operating temper ature range of -10c to +70c. extended temperature crystals are available at a much higher cost.
pic18f2331/2431/4331/4431 ds39616b-page 342 preliminary ? 2003 microchip technology inc. pic18lf2x31/4x31 410 600 a -40c f osc = 4 mhz ( rc_run mode, internal oscillator source) 425 600 a 25c v dd = 2.0v 435 600 a 85c pic18lf2x31/4x31 650 900 a -40c 670 900 a 25c v dd = 3.0v 680 900 a 85c all devices 1.2 1.8 ma -40c 1.2 1.8 ma 25c v dd = 5.0v 1.2 1.8 ma 85c 25.2 dc characteristics: power-down and supply current pic18f2331/2431/4331/4431 (industrial, extended) pic18lf2331/2431/4331/4431 (industrial) (continued) pic18f2331/2431/4331/4431 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf2331/2431/4331/4431 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. device typ max units conditions legend: shading of rows is to assist in readability of the table. note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-ra il; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt enabled/disabled as specified. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be estimated by the formula ir = v dd /2r ext (ma) with r ext in k ? . 4: standard low cost 32 khz crystals have an operating temper ature range of -10c to +70c. extended temperature crystals are available at a much higher cost.
? 2003 microchip technology inc. preliminary ds39616b-page 343 pic18f2331/2431/4331/4431 supply current (i dd ) (2,3) pic18lf2x31/4x31 4.7 8 a -40c f osc = 31 khz ( rc_idle mode, internal oscillator source) 5.0 8 a 25c v dd = 2.0v 5.8 11 a 85c pic18lf2x31/4x31 7.0 11 a -40c 7.8 11 a 25c v dd = 3.0v 8.7 15 a 85c all devices 12 16 a -40c 14 16 a 25c v dd = 5.0v 14 22 a 85c pic18lf2x31/4x31 75 150 a -40c f osc = 1 mhz ( rc_idle mode, internal oscillator source) 85 150 a 25c v dd = 2.0v 95 150 a 85c pic18lf2x31/4x31 110 180 a -40c 125 180 a 25c v dd = 3.0v 135 180 a 85c all devices 180 300 a -40c 195 300 a 25c v dd = 5.0v 200 300 a 85c 25.2 dc characteristics: power-down and supply current pic18f2331/2431/4331/4431 (industrial, extended) pic18lf2331/2431/4331/4431 (industrial) (continued) pic18f2331/2431/4331/4431 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf2331/2431/4331/4431 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. device typ max units conditions legend: shading of rows is to assist in readability of the table. note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-ra il; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt enabled/disabled as specified. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be estimated by the formula ir = v dd /2r ext (ma) with r ext in k ? . 4: standard low cost 32 khz crystals have an operating temper ature range of -10c to +70c. extended temperature crystals are available at a much higher cost.
pic18f2331/2431/4331/4431 ds39616b-page 344 preliminary ? 2003 microchip technology inc. pic18lf2x31/4x31 175 275 a -40c f osc = 4 mhz ( rc_idle mode, internal oscillator source) 185 275 a 25c v dd = 2.0v 195 275 a 85c pic18lf2x31/4x31 265 375 a -40c 280 375 a 25c v dd = 3.0v 300 375 a 85c all devices 475 800 a -40c 500 800 a 25c v dd = 5.0v 505 800 a 85c 25.2 dc characteristics: power-down and supply current pic18f2331/2431/4331/4431 (industrial, extended) pic18lf2331/2431/4331/4431 (industrial) (continued) pic18f2331/2431/4331/4431 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf2331/2431/4331/4431 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. device typ max units conditions legend: shading of rows is to assist in readability of the table. note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-ra il; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt enabled/disabled as specified. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be estimated by the formula ir = v dd /2r ext (ma) with r ext in k ? . 4: standard low cost 32 khz crystals have an operating temper ature range of -10c to +70c. extended temperature crystals are available at a much higher cost.
? 2003 microchip technology inc. preliminary ds39616b-page 345 pic18f2331/2431/4331/4431 supply current (i dd ) (2,3) pic18lf2x31/4x31 150 250 a -40c f osc = 1 mh z ( pri_run , ec oscillator) 150 250 a 25c v dd = 2.0v 160 250 a 85c pic18lf2x31/4x31 340 350 a -40c 300 350 a 25c v dd = 3.0v 280 350 a 85c all devices 0.72 1.0 ma -40c 0.63 1.0 ma 25c v dd = 5.0v 0.57 1.0 ma 85c pic18lf2x31/4x31 440 600 a -40c f osc = 4 mhz ( pri_run , ec oscillator) 450 600 a 25c v dd = 2.0v 460 600 a 85c pic18lf2x31/4x31 0.80 1.0 ma -40c 0.78 1.0 ma 25c v dd = 3.0v 0.77 1.0 ma 85c all devices 1.6 2.0 ma -40c 1.5 2.0 ma 25c v dd = 5.0v 1.5 2.0 ma 85c all devices 9.5 12 ma -40c f osc = 40 mh z ( pri_run , ec oscillator) 9.7 12 ma 25c v dd = 4.2v 9.9 12 ma 85c all devices 11.9 15 ma -40c 12.1 15 ma 25c v dd = 5.0v 12.3 15 ma 85c 25.2 dc characteristics: power-down and supply current pic18f2331/2431/4331/4431 (industrial, extended) pic18lf2331/2431/4331/4431 (industrial) (continued) pic18f2331/2431/4331/4431 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf2331/2431/4331/4431 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. device typ max units conditions legend: shading of rows is to assist in readability of the table. note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-ra il; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt enabled/disabled as specified. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be estimated by the formula ir = v dd /2r ext (ma) with r ext in k ? . 4: standard low cost 32 khz crystals have an operating temper ature range of -10c to +70c. extended temperature crystals are available at a much higher cost.
pic18f2331/2431/4331/4431 ds39616b-page 346 preliminary ? 2003 microchip technology inc. supply current (i dd ) (2,3) pic18lf2x31/4x31 35 50 a -40c f osc = 1 mhz ( pri_idle mode, ec oscillator) 35 50 a 25c v dd = 2.0v 35 60 a 85c pic18lf2x31/4x31 55 80 a -40c 50 80 a 25c v dd = 3.0v 60 100 a 85c all devices 105 150 a -40c 110 150 a 25c v dd = 5.0v 115 150 a 85c pic18lf2x31/4x31 135 180 a -40c f osc = 4 mhz ( pri_idle mode, ec oscillator) 140 180 a 25c v dd = 2.0v 140 180 a 85c pic18lf2x31/4x31 215 280 a -40c 225 280 a 25c v dd = 3.0v 230 280 a 85c all devices 410 525 a -40c 420 525 a 25c v dd = 5.0v 430 525 a 85c all devices 3.2 4.1 ma -40c f osc = 40 mhz ( pri_idle mode, ec oscillator) 3.2 4.1 ma 25c v dd = 4.2 v 3.3 4.1 ma 85c all devices 4.0 5.1 ma -40c 4.1 5.1 ma 25c v dd = 5.0v 4.1 5.1 ma 85c 25.2 dc characteristics: power-down and supply current pic18f2331/2431/4331/4431 (industrial, extended) pic18lf2331/2431/4331/4431 (industrial) (continued) pic18f2331/2431/4331/4431 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf2331/2431/4331/4431 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. device typ max units conditions legend: shading of rows is to assist in readability of the table. note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-ra il; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt enabled/disabled as specified. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be estimated by the formula ir = v dd /2r ext (ma) with r ext in k ? . 4: standard low cost 32 khz crystals have an operating temper ature range of -10c to +70c. extended temperature crystals are available at a much higher cost.
? 2003 microchip technology inc. preliminary ds39616b-page 347 pic18f2331/2431/4331/4431 supply current (i dd ) (2,3) pic18lf2x31/4x31 5.1 9 a -10c f osc = 32 khz (4) ( sec_run mode, timer1 as clock) 5.8 9 a 25c v dd = 2.0v 7.9 11 a 70c pic18lf2x31/4x31 7.9 12 a -10c 8.9 12 a 25c v dd = 3.0v 10.5 14 a 70c all devices 12.5 20 a -10c 16.3 20 a 25c v dd = 5.0v 18.9 25 a 70c supply current (i dd ) (2,3) pic18lf2x31/4x31 9.2 15 a -10c f osc = 32 khz (4) ( sec_idle mode, timer1 as clock) 9.6 15 a 25c v dd = 2.0v 12.7 18 a 70c pic18lf2x31/4x31 22.0 30 a -10c 21.0 30 a 25c v dd = 3.0v 20.0 35 a 70c all devices 30 80 a -10c 45 80 a 25c v dd = 5.0v 45 85 a 70c 25.2 dc characteristics: power-down and supply current pic18f2331/2431/4331/4431 (industrial, extended) pic18lf2331/2431/4331/4431 (industrial) (continued) pic18f2331/2431/4331/4431 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf2331/2431/4331/4431 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. device typ max units conditions legend: shading of rows is to assist in readability of the table. note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-ra il; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt enabled/disabled as specified. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be estimated by the formula ir = v dd /2r ext (ma) with r ext in k ? . 4: standard low cost 32 khz crystals have an operating temper ature range of -10c to +70c. extended temperature crystals are available at a much higher cost.
pic18f2331/2431/4331/4431 ds39616b-page 348 preliminary ? 2003 microchip technology inc. module differential currents ( ? i wdt , ? i bor , ? i lvd , ? i oscb , ? i ad ) d022 ( ? i wdt ) watchdog timer 1.5 4.0 a -40c 2.2 4.0 a 25c v dd = 2.0v 3.1 5.0 a 85c 2.5 6.0 a -40c 3.3 6.0 a 25c v dd = 3.0v 4.7 7.0 a 85c 3.7 10.0 a -40c 4.5 10.0 a 25c v dd = 5.0v 6.1 13.0 a 85c d022a brown-out reset 19 35.0 a-40 c to +85 c v dd = 3.0v ( ? i bor ) 24 45.0 a-40 c to +85 c v dd = 5.0v d022b low-voltage detect 8.5 25.0 a-40 c to +85 c v dd = 2.0v ( ? i lvd ) 16 35.0 a-40 c to +85 c v dd = 3.0v 20 45.0 a-40 c to +85 c v dd = 5.0v d025 timer1 oscillator 1.7 3.5 a-40 c ( ? i oscb )1.83.5 a25 c v dd = 2.0v 32 khz on timer1 (4) 2.1 4.5 a85 c 2.2 4.5 a-40 c 2.6 4.5 a25 c v dd = 3.0v 32 khz on timer1 (4) 2.8 5.5 a85 c 3.0 6.0 a-40 c 3.3 6.0 a25 c v dd = 5.0v 32 khz on timer1 (4) 3.6 7.0 a85 c d026 ( ? i ad ) a/d converter 1.0 3.0 a-40 c to 85 c v dd = 2.0v 1.0 4.0 a-40 c to 85 c v dd = 3.0v a/d on, not converting 2.0 10.0 a-40 c to 85 c v dd = 5.0v 25.2 dc characteristics: power-down and supply current pic18f2331/2431/4331/4431 (industrial, extended) pic18lf2331/2431/4331/4431 (industrial) (continued) pic18f2331/2431/4331/4431 (industrial, extended) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf2331/2431/4331/4431 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. device typ max units conditions legend: shading of rows is to assist in readability of the table. note 1: the power-down current in sleep mode does not depend on the oscillator type. power-down current is measured with the part in sleep mode, with all i/o pins in high-impedance state and tied to v dd or v ss , and all features that add delta current disabled (such as wdt, timer1 oscillator, bor, etc.). 2: the supply current is mainly a function of operating voltage, frequency and mode. other factors, such as i/o pin loading and switching rate, oscillator type and circuit, internal code execution pattern and temperature, also have an impact on the current consumption. the test conditions for all i dd measurements in active operation mode are: osc1 = external square wave, from rail-to-ra il; all i/o pins tri-stated, pulled to v dd ; mclr = v dd ; wdt enabled/disabled as specified. 3: for rc oscillator configurations, current through r ext is not included. the current through the resistor can be estimated by the formula ir = v dd /2r ext (ma) with r ext in k ? . 4: standard low cost 32 khz crystals have an operating temper ature range of -10c to +70c. extended temperature crystals are available at a much higher cost.
? 2003 microchip technology inc. preliminary ds39616b-page 349 pic18f2331/2431/4331/4431 25.3 dc characteristics: pic18f2331/2431/4331/4431 (industrial) pic18lf2331/2431/4331/4431 (industrial) dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. symbol characteristic min max units conditions v il input low voltage i/o ports: d030 with ttl buffer v ss 0.15 v dd vv dd < 4.5v d030a ? 0.8 v 4.5v v dd 5.5v d031 with schmitt trigger buffer rc3 and rc4 v ss v ss 0.2 v dd 0.3 v dd v v d032 mclr v ss 0.2 v dd v d032a osc1 and t1osi v ss 0.3 v dd v lp, xt, hs, hspll modes (1) d033 osc1 v ss 0.2 v dd vec mode (1) v ih input high voltage i/o ports: d040 with ttl buffer 0.25 v dd + 0.8v v dd vv dd < 4.5v d040a 2.0 v dd v4.5v v dd 5.5v d041 with schmitt trigger buffer rc3 and rc4 0.8 v dd 0.7 v dd v dd v dd v v d042 mclr 0.8 v dd v dd v d042a osc1 and t1osi 0.7 v dd v dd v lp, xt, hs, hspll modes (1) d043 osc1 0.8 v dd v dd vec mode (1) i il input leakage current (2,3) d060 i/o ports ? 1 av ss v pin v dd , pin at hi-impedance d061 mclr ? 1 avss v pin v dd d063 osc1 ? 1 avss v pin v dd i pu weak pull-up current d070 i purb portb weak pull-up current 50 400 av dd = 5v, v pin = v ss note 1: in rc oscillator configuration, the osc1/clki pin is a schmitt trigger input. it is not recommended that the picmicro device be driven with an external clock while in rc mode. 2: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 3: negative current is defined as current sourced by the pin. 4: parameter is characterized but not tested.
pic18f2331/2431/4331/4431 ds39616b-page 350 preliminary ? 2003 microchip technology inc. v ol output low voltage d080 i/o ports ? 0.6 v i ol = 8.5 ma, v dd = 4.5v, -40 c to +85 c d083 osc2/clko (rc, rcio, ec, ecio modes) ?0.6vi ol = 1.6 ma, v dd = 4.5v, -40 c to +85 c v oh output high voltage (3) d090 i/o ports v dd ? 0.7 ? v i oh = -3.0 ma, v dd = 4.5v, -40 c to +85 c d092 osc2/clko (rc, rcio, ec, ecio modes) v dd ? 0.7 ? v i oh = -1.3 ma, v dd = 4.5v, -40 c to +85 c d150 v od open-drain high voltage ? 8.5 v ra4 pin capacitive loading specs on output pins d100 (4) c osc2 osc2 pin ? 15 pf in xt, hs and lp modes when external clock is used to drive osc1 d101 c io all i/o pins and osc2 (in rc mode) ? 50 pf to meet the ac timing specifications d102 c b scl, sda ? 400 pf i 2 c? specification 25.3 dc characteristics: pic18f2331/2431/4331/4431 (industrial) pic18lf2331/2431/4331/4431 (industrial) (continued) dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. symbol characteristic min max units conditions note 1: in rc oscillator configuration, the osc1/clki pin is a schmitt trigger input. it is not recommended that the picmicro device be driven with an external clock while in rc mode. 2: the leakage current on the mclr pin is strongly dependent on the applied voltage level. the specified levels represent normal operating conditions. higher leakage current may be measured at different input voltages. 3: negative current is defined as current sourced by the pin. 4: parameter is characterized but not tested.
? 2003 microchip technology inc. preliminary ds39616b-page 351 pic18f2331/2431/4331/4431 table 25-1: memory programming requirements dc characteristics standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. sym characteristic min typ? max units conditions internal program memory programming specifications (1) d110 v pp voltage on mclr /v pp pin 9.00 ? 13.25 v (note 3) d112 i pp current into mclr /v pp pin ? ? 300 a d113 i ddp supply current during programming ?? 1ma data eeprom memory d120 e d byte endurance 100k 1m ? e/w -40 c to +85 c d121 v drw v dd for read/write v min ? 5.5 v using eecon to read/write v min = minimum operating voltage d122 t dew erase/write cycle time ? 4 ? ms d123 t retd characteristic retention 40 ? ? year provided no other specifications are violated d124 t ref number of total erase/write cycles before refresh (2) 1m 10m ? e/w -40c to +85c program flash memory d130 e p cell endurance 10k 100k ? e/w -40 c to +85 c d131 v pr v dd for read v min ? 5.5 v v min = minimum operating voltage d132 v ie v dd for block erase 4.5 ? 5.5 v using icsp port d132a v iw v dd for externally timed erase or write 4.5 ? 5.5 v using icsp port d132b v pew v dd for self-timed write v min ? 5.5 v v min = minimum operating voltage d133 t ie icsp block erase cycle time ? 4 ? ms v dd > 4.5v d133a t iw icsp erase or write cycle time (externally timed) 1??msv dd > 4.5v d133a t iw self-timed write cycle time ? 2 ? ms d134 t retd characteristic retention 40 100 ? year provided no other specifications are violated ? data in ?typ? column is at 5.0v, 25c unless otherwise stated. these parameters are for design guidance only and are not tested. note 1: these specifications are for programming the on-chip program memory through the use of table write instructions. 2: refer to section 7.8 ?using the data eeprom? for a more detailed discussion on data eeprom endurance. 3: required only if low-voltage programming is disabled.
pic18f2331/2431/4331/4431 ds39616b-page 352 preliminary ? 2003 microchip technology inc. figure 25-3: low-voltage detect characteristics table 25-2: low-voltage detect characteristics v lvd lvdif v dd (lvdif set by hardware) (lvdif can be cleared in software) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. symbol characteristic min typ? max units conditions d420 lvd voltage on v dd transition high to low lvv = 0010 2.08 2.26 2.44 v lvv = 0011 2.26 2.45 2.65 v lvv = 0100 2.35 2.55 2.76 v lvv = 0101 2.55 2.77 2.99 v lvv = 0110 2.64 2.87 3.10 v lvv = 0111 2.82 3.07 3.31 v lvv = 1000 3.09 3.36 3.63 v lvv = 1001 3.29 3.57 3.86 v lvv = 1010 3.38 3.67 3.96 v lvv = 1011 3.56 3.87 4.18 v lvv = 1100 3.75 4.07 4.40 v lvv = 1101 3.93 4.28 4.62 v lvv = 1110 4.23 4.60 4.96 v ? production tested at t amb = 25c. specifications over temp. limits ensured by characterization.
? 2003 microchip technology inc. preliminary ds39616b-page 353 pic18f2331/2431/4331/4431 25.4 ac (timing) characteristics 25.4.1 timing parameter symbology the timing parameter symbols have been created following one of the following formats: 1. tpps2pps 3. t cc : st (i 2 c specifications only) 2. tpps 4. ts (i 2 c specifications only) t f frequency t time lowercase letters (pp) and their meanings: pp cc ccp1 osc osc1 ck clko rd rd cs cs rw rd or wr di sdi sc sck do sdo ss ss dt data in t0 t0cki io i/o port t1 t1cki mc mclr wr wr uppercase letters and their meanings: s f fall p period hhigh rrise i invalid (hi-impedance) v valid l low z hi-impedance i 2 c only aa output access high high buf bus free low low t cc : st (i 2 c specifications only) cc hd hold su setup st dat data input hold sto stop condition sta start condition
pic18f2331/2431/4331/4431 ds39616b-page 354 preliminary ? 2003 microchip technology inc. 25.4.2 timing conditions the temperature and voltages specified in table 25-3 apply to all timing specifications unless otherwise noted. figure 25-4 specifies the load conditions for the timing specifications. table 25-3: temperature and voltage specifications - ac figure 25-4: load conditions for devi ce timing specifications note: because of space limitations, the generic terms ?pic18fxx31? and ?pic18lfxx31? are used throughout this section to refer to the pic18f2331/2431/4331/4431 and pic18lf2331/2431/4331/4431 families of devices specifically, and only those devices. ac characteristics standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial operating voltage v dd range as described in dc spec section 25.1 and section 25.3 . lf parts operate for industrial temperatures only. v dd /2 c l r l pin pin v ss v ss c l r l =464 ? c l = 50 pf for all pins except osc2/clko and including d and e outputs as ports load condition 1 load condition 2
? 2003 microchip technology inc. preliminary ds39616b-page 355 pic18f2331/2431/4331/4431 25.4.3 timing diagrams and specifications figure 25-5: external clock timing (all modes except pll) table 25-4: external clock timing requirements osc1 clko q4 q1 q2 q3 q4 q1 1 2 3 3 4 4 param. no. symbol characteristic min max units conditions 1a f osc external clki frequency (1) dc 40 mhz ec, ecio oscillator frequency (1) dc 4 mhz rc osc 0.1 4 mhz xt osc 4 25 mhz hs osc 4 10 mhz hs + pll osc 5 200 khz lp osc mode 1t osc external clki period (1) 25 ? ns ec, ecio oscillator period (1) 250 ? ns rc osc 250 10,000 ns xt osc 25 100 250 250 ns ns hs osc hs + pll osc 25 ? slp osc 2t cy instruction cycle time (1) 100 ? ns t cy = 4/f osc 3 tosl, to s h external clock in (osc1) high or low time 30 ? ns xt osc 2.5 ? slp osc 10 ? ns hs osc 4tosr, to s f external clock in (osc1) rise or fall time ? 20 ns xt osc ? 50 ns lp osc ?7.5nshs osc note 1: instruction cycle period (t cy ) equals four times the input oscillator time base period for all configurations except pll. all specified values are based on characterization data for that particular oscillator type under standard operating conditions with the device executing code. exceeding these specified limits may result in an unstable oscillator operation and/or higher than expected current consumption. all devices are tested to operate at ?min.? values with an external clock applied to the osc1/clki pin. when an external clock input is used, the ?max.? cycle time limit is ?dc? (no clock) for all devices.
pic18f2331/2431/4331/4431 ds39616b-page 356 preliminary ? 2003 microchip technology inc. table 25-5: pll clock timing specifications (v dd = 4.2v to 5.5v) param no. sym characteristic min typ? max units conditions f10 f osc oscillator frequency range 4 ? 10 mhz hs mode only f11 f sys on-chip vco system frequency 16 ? 40 mhz hs mode only f12 t pll pll start-up time (lock time) ? ? 2 ms f13 ? clk clko stability (jitter) -2 ? +2 % ? data in ?typ? column is at 5v, 25 c unless otherwise stated. these parameters are for design guidance only and are not tested. table 25-6: internal rc accuracy pic18f2331/2431/4331/4431 (industrial) pic18lf2331/2431/4331/4431 (industrial) pic18f1220/1320 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial pic18lf1220/1320 (industrial) standard operating conditions (unless otherwise stated) operating temperature -40c t a +85c for industrial param no. device min typ max units conditions intosc accuracy @ freq = 8 mhz, 4 mhz, 2 mhz, 1 mhz, 500 khz, 250 khz, 125 khz (1) f2 pic18lf2331/2431/4331/4431 -15 +/-5 +15 % 25c v dd = 3.0v f3 all devices -15 +/-5 +15 % 25c v dd = 5.0v intrc accuracy @ freq = 31 khz (2) f5 pic18lf2331/2431/4331/4431 26.562 ? 35.938 khz 25c v dd = 3.0v f6 all devices 26.562 ? 35.938 khz 25c v dd = 5.0v intrc stability (3) f8 pic18lf2331/2431/4331/4431 tbd 1 tbd % 25c v dd = 3.0v f9 all devices tbd 1 tbd % 25c v dd = 5.0v legend: shading of rows is to assist in readability of the table. note 1: frequency calibrated at 25c. osctune register can be used to compensate for temperature drift. 2: intrc frequency after calibration. 3: change of intrc frequency as v dd changes.
? 2003 microchip technology inc. preliminary ds39616b-page 357 pic18f2331/2431/4331/4431 figure 25-6: clko and i/o timing table 25-7: clko and i/o timing requirements note: refer to figure 25-4 for load conditions. osc1 clko i/o pin (input) i/o pin (output) q4 q1 q2 q3 10 13 14 17 20, 21 19 18 15 11 12 16 old value new value param no. symbol characteristic min typ max units conditions 10 tosh2ckl osc1 to clko ? 75 200 ns (1) 11 tosh2ckh osc1 to clko ? 75 200 ns (1) 12 tckr clko rise time ? 35 100 ns (1) 13 tckf clko fall time ? 35 100 ns (1) 14 tckl2iov clko to port out valid ? ? 0.5 t cy + 20 ns (1) 15 tiov2ckh port in valid before clko 0.25 t cy + 25 ? ? ns (1) 16 tckh2ioi port in hold after clko 0??ns (1) 17 tosh2iov osc1 (q1 cycle) to port out valid ? 50 150 ns 18 tosh2ioi osc1 (q2 cycle) to port input invalid (i/o in hold time) pic18fxx31 100 ? ? ns 18a pic18lfxx31 200 ? ? ns 19 tiov2osh port input valid to osc1 (i/o in setup time) 0 ? ? ns 20 tior port output rise time pic18fxx31 ? 10 25 ns 20a pic18lfxx31 ? ? 60 ns 21 tiof port output fall time pic18fxx31 ? 10 25 ns 21a pic18lfxx31 ? ? 60 ns 22?? t inp int pin high or low time t cy ??ns 23?? t rbp rb7:rb4 change int high or low time t cy ??ns 24?? t rcp rb7:rb4 change int high or low time 20 ns ?? these parameters are asynchronous events not related to any internal clock edges. note 1: measurements are taken in rc mode, where clko output is 4 x t osc .
pic18f2331/2431/4331/4431 ds39616b-page 358 preliminary ? 2003 microchip technology inc. figure 25-7: reset, watchdog timer, oscillator start-up timer and power-up timer timing figure 25-8: brown-out reset timing table 25-8: reset, watchdog timer, oscillator start-up timer, power-up timer and brown-out reset requirements param. no. symbol characteristic min typ max units conditions 30 tmcl mclr pulse width (low) 2 ? ? s 31 t wdt watchdog timer time-out period (no postscaler) ?4.00tbd ms 32 t ost oscillation start-up timer period 1024 t osc ? 1024 t osc ?t osc = osc1 period 33 t pwrt power-up timer period ? 65.5 tbd ms 34 t ioz i/o hi-impedance from mclr low or watchdog timer reset ?2? s 35 t bor brown-out reset pulse width 200 ? ? sv dd b vdd (see d005) 36 t ivrst time for internal reference voltage to become stable ?2050 s 37 t lvd low-voltage detect pulse width 200 ? ? sv dd v lvd v dd mclr internal por pwrt time-out osc time-out internal reset watchdog timer reset 33 32 30 31 34 i/o pins 34 note: refer to figure 25-4 for load conditions. v dd bv dd 35 v bgap = 1.2v v irvst enable internal reference voltage internal reference voltage stable 36
? 2003 microchip technology inc. preliminary ds39616b-page 359 pic18f2331/2431/4331/4431 figure 25-9: timer0 and timer1 external clock timings table 25-9: timer0 and timer1 external clock requirements note: refer to figure 25-4 for load conditions. 46 47 45 48 41 42 40 t0cki t1oso/t1cki tmr0 or tmr1 param no. symbol characteristic min max units conditions 40 tt0h t0cki high pulse width no prescaler 0.5 t cy + 20 ? ns with prescaler 10 ? ns 41 tt0l t0cki low pulse width no prescaler 0.5 t cy + 20 ? ns with prescaler 10 ? ns 42 tt0p t0cki period no prescaler t cy + 10 ? ns with prescaler greater of: 20 ns or t cy + 40 n ?nsn = prescale value (1, 2, 4,..., 256) 45 tt1h t1cki high time synchronous, no prescaler 0.5 t cy + 20 ? ns synchronous, with prescaler pic18fxx31 10 ? ns pic18lfxx31 25 ? ns asynchronous pic18fxx31 30 ? ns pic18lfxx31 50 ? ns 46 tt1l t1cki low time synchronous, no prescaler 0.5 t cy + 5 ? ns synchronous, with prescaler pic18fxx31 10 ? ns pic18lfxx31 25 ? ns asynchronous pic18fxx31 30 ? ns pic18lfxx31 tbd tbd ns 47 tt1p t1cki input period synchronous greater of: 20 ns or t cy + 40 n ?nsn = prescale value (1, 2, 4, 8) asynchronous 60 ? ns ft1 t1cki oscillator input frequency range dc 50 khz 48 tcke2tmri delay from external t1cki clock edge to timer increment 2 t osc 7 t osc ?
pic18f2331/2431/4331/4431 ds39616b-page 360 preliminary ? 2003 microchip technology inc. figure 25-10: capture/compare/pwm timings (all ccp modules) table 25-10: capture/compare/pwm requirements (all ccp modules) note: refer to figure 25-4 for load conditions. ccpx (capture mode) 50 51 52 ccpx 53 54 (compare or pwm mode) param no. symbol characteristic min max units conditions 50 tccl ccpx input low time no prescaler 0.5 t cy + 20 ? ns with prescaler pic18fxx31 10 ? ns pic18lfxx31 20 ? ns 51 tcch ccpx input high time no prescaler 0.5 t cy + 20 ? ns with prescaler pic18fxx31 10 ? ns pic18lfxx31 20 ? ns 52 tccp ccpx input period 3 t cy + 40 n ?nsn = prescale value (1,4 or 16) 53 tccr ccpx output fall time pic18fxx31 ? 25 ns pic18lfxx31 ? 45 ns 54 tccf ccpx output fall time pic18fxx31 ? 25 ns pic18lfxx31 ? 45 ns
? 2003 microchip technology inc. preliminary ds39616b-page 361 pic18f2331/2431/4331/4431 figure 25-11: example spi master mode timing (cke = 0) table 25-11: example spi mode requirements (master mode, cke = 0) ss sck (ckp = 0) sck (ckp = 1) sdo sdi 70 71 72 73 74 75, 76 78 79 80 79 78 msb lsb bit6 - - - - - -1 msb in lsb in bit6 - - - -1 note: refer to figure 25-4 for load conditions. param no. symbol characteristic min max units conditions 70 tssl2sch, tssl2scl ss to sck or sck input t cy ?ns 71 tsch sck input high time (slave mode) continuous 1.25 t cy + 30 ? ns 71a single byte 40 ? ns (note 1) 72 tscl sck input low time (slave mode) continuous 1.25 t cy + 30 ? ns 72a single byte 40 ? ns (note 1) 73 tdiv2sch, tdiv2scl setup time of sdi data input to sck edge 100 ? ns 73a t b 2 b last clock edge of byte1 to the 1st clock edge of byte2 1.5 t cy + 40 ? ns (note 2) 74 tsch2dil, ts c l 2 d i l hold time of sdi data input to sck edge 100 ? ns 75 tdor sdo data output rise time pic18fxx31 ? 25 ns pic18lfxx31 ? 45 ns 76 tdof sdo data output fall time ? 25 ns 78 tscr sck output rise time (master mode) pic18fxx31 ? 25 ns pic18lfxx31 ? 45 ns 79 tscf sck output fall time (master mode) ? 25 ns 80 tsch2dov, tscl2dov sdo data output valid after sck edge pic18fxx31 ? 50 ns pic18lfxx31 ? 100 ns note 1: requires the use of parameter # 73a. 2: only if parameter # 71a and # 72a are used.
pic18f2331/2431/4331/4431 ds39616b-page 362 preliminary ? 2003 microchip technology inc. figure 25-12: example spi master mode timing (cke = 1) table 25-12: example spi mode requirements (master mode, cke = 1) ss sck (ckp = 0) sck (ckp = 1) sdo sdi 81 71 72 74 75, 76 78 80 msb 79 73 msb in bit6 - - - - - -1 lsb in bit6 - - - -1 lsb note: refer to figure 25-4 for load conditions. param. no. symbol characteristic min max units conditions 71 tsch sck input high time (slave mode) continuous 1.25 t cy + 30 ? ns 71a single byte 40 ? ns (note 1) 72 tscl sck input low time (slave mode) continuous 1.25 t cy + 30 ? ns 72a single byte 40 ? ns (note 1) 73 tdiv2sch, tdiv2scl setup time of sdi data input to sck edge 100 ? ns 73a t b 2 b last clock edge of byte1 to the 1st clock edge of byte2 1.5 t cy + 40 ? ns (note 2) 74 tsch2dil, ts c l 2 d i l hold time of sdi data input to sck edge 100 ? ns 75 tdor sdo data output rise time pic18fxx31 ? 25 ns pic18lfxx31 45 ns 76 tdof sdo data output fall time ? 25 ns 78 tscr sck output rise time (master mode) pic18fxx31 ? 25 ns pic18lfxx31 45 ns 79 tscf sck output fall time (master mode) ? 25 ns 80 tsch2dov, tscl2dov sdo data output valid after sck edge pic18fxx31 ? 50 ns pic18lfxx31 100 ns 81 tdov2sch, tdov2scl sdo data output setup to sck edge t cy ?ns note 1: requires the use of parameter # 73a. 2: only if parameter # 71a and # 72a are used.
? 2003 microchip technology inc. preliminary ds39616b-page 363 pic18f2331/2431/4331/4431 figure 25-13: example spi slave mode timing (cke = 0) table 25-13: example spi mode requirements (slave mode timing (cke = 0)) param no. symbol characteristic min max units conditions 70 tssl2sch, tssl2scl ss to sck or sck input t cy ?ns 71 tsch sck input high time (slave mode) continuous 1.25 t cy + 30 ? ns 71a single byte 40 ? ns (note 1) 72 tscl sck input low time (slave mode) continuous 1.25 t cy + 30 ? ns 72a single byte 40 ? ns (note 1) 73 tdiv2sch, tdiv2scl setup time of sdi data input to sck edge 100 ? ns 73a t b 2 b last clock edge of byte1 to the first clock edge of byte2 1.5 t cy + 40 ? ns (note 2) 74 tsch2dil, tscl2dil hold time of sdi data input to sck edge 100 ? ns 75 tdor sdo data output rise time pic18fxx31 ? 25 ns pic18lfxx31 45 ns 76 tdof sdo data output fall time ? 25 ns 77 tssh2doz ss to sdo output hi-impedance 10 50 ns 78 tscr sck output rise time (master mode) pic18fxx31 ? 25 ns pic18lfxx31 45 ns 79 tscf sck output fall time (master mode) ? 25 ns 80 tsch2dov, tscl2dov sdo data output valid after sck edge pic18fxx31 ? 50 ns pic18lfxx31 100 ns 83 tsch2ssh, tscl2ssh ss after sck edge 1.5 t cy + 40 ? ns note 1: requires the use of parameter # 73a. 2: only if parameter # 71a and # 72a are used. ss sck (ckp = 0) sck (ckp = 1) sdo sdi 70 71 72 73 74 75, 76 77 78 79 80 79 78 sdi msb lsb bit6 - - - - - -1 msb in bit6 - - - -1 lsb in 83 note: refer to figure 25-4 for load conditions.
pic18f2331/2431/4331/4431 ds39616b-page 364 preliminary ? 2003 microchip technology inc. figure 25-14: example spi slave mode timing (cke = 1) table 25-14: example spi slave mode requirements (cke = 1) param no. symbol characteristic min max units conditions 70 tssl2sch, tssl2scl ss to sck or sck input t cy ?ns 71 tsch sck input high time (slave mode) continuous 1.25 t cy + 30 ? ns 71a single byte 40 ? ns (note 1) 72 tscl sck input low time (slave mode) continuous 1.25 t cy + 30 ? ns 72a single byte 40 ? ns (note 1) 73a t b 2 b last clock edge of byte1 to the first clock edge of byte2 1.5 t cy + 40 ? ns (note 2) 74 tsch2dil, tscl2dil hold time of sdi data input to sck edge 100 ?ns 75 tdor sdo data output rise time pic18fxx31 ? 25 ns pic18lfxx31 45 ns 76 tdof sdo data output fall time ? 25 ns 77 tssh2doz ss to sdo output hi-impedance 10 50 ns 78 tscr sck output rise time (master mode) pic18fxx31 ? 25 ns pic18lfxx31 ? 45 ns 79 tscf sck output fall time (master mode) ? 25 ns 80 tsch2dov, tscl2dov sdo data output valid after sck edge pic18fxx31 ? 50 ns pic18lfxx31 ? 100 ns 82 tssl2dov sdo data output valid after ss edge pic18fxx31 ? 50 ns pic18lfxx31 ? 100 ns 83 tsch2ssh, tscl2ssh ss after sck edge 1.5 t cy + 40 ?ns note 1: requires the use of parameter # 73a. 2: only if parameter # 71a and # 72a are used. ss sck (ckp = 0) sck (ckp = 1) sdo sdi 70 71 72 82 sdi 74 75, 76 msb bit6 - - - - - -1 lsb 77 msb in bit6 - - - -1 lsb in 80 83 note: refer to figure 25-4 for load conditions.
? 2003 microchip technology inc. preliminary ds39616b-page 365 pic18f2331/2431/4331/4431 figure 25-15: i 2 c bus start/stop bits timing table 25-15: i 2 c bus start/stop bits requirements (slave mode) figure 25-16: i 2 c bus data timing note: refer to figure 25-4 for load conditions. 91 92 93 scl sda start condition stop condition 90 param. no. symbol characteristic min max units conditions 90 t su : sta start condition 100 khz mode 4700 ? ns only relevant for repeated start condition setup time 400 khz mode 600 ? 91 t hd : sta start condition 100 khz mode 4000 ? ns after this period, the first clock pulse is generated hold time 400 khz mode 600 ? 92 t su : sto stop condition 100 khz mode 4700 ? ns setup time 400 khz mode 600 ? 93 t hd : sto stop condition 100 khz mode 4000 ? ns hold time 400 khz mode 600 ? note: refer to figure 25-4 for load conditions. 90 91 92 100 101 103 106 107 109 109 110 102 scl sda in sda out
pic18f2331/2431/4331/4431 ds39616b-page 366 preliminary ? 2003 microchip technology inc. table 25-16: i 2 c bus data requirements (slave mode) param. no. symbol characteristic min max units conditions 100 t high clock high time 100 khz mode 4.0 ? s pic18fxx31 must operate at a minimum of 1.5 mhz 400 khz mode 0.6 ? s pic18fxx31 must operate at a minimum of 10 mhz ssp module 1.5 t cy ? 101 t low clock low time 100 khz mode 4.7 ? s pic18fxx31 must operate at a minimum of 1.5 mhz 400 khz mode 1.3 ? s pic18fxx31 must operate at a minimum of 10 mhz ssp module 1.5 t cy ? 102 t r sda and scl rise time 100 khz mode ? 1000 ns 400 khz mode 20 + 0.1 c b 300 ns c b is specified to be from 10 to 400 pf 103 t f sda and scl fall time 100 khz mode ? 300 ns 400 khz mode 20 + 0.1 c b 300 ns c b is specified to be from 10 to 400 pf 90 t su : sta start condition setup time 100 khz mode 4.7 ? s only relevant for repeated start condition 400 khz mode 0.6 ? s 91 t hd : sta start condition hold time 100 khz mode 4.0 ? s after this period, the first clock pulse is generated 400 khz mode 0.6 ? s 106 t hd : dat data input hold time 100 khz mode 0 ? ns 400 khz mode 0 0.9 s 107 t su : dat data input setup time 100 khz mode 250 ? ns (note 2) 400 khz mode 100 ? ns 92 t su : sto stop condition setup time 100 khz mode 4.7 ? s 400 khz mode 0.6 ? s 109 t aa output valid from clock 100 khz mode ? 3500 ns (note 1) 400 khz mode ? ? ns 110 t buf bus free time 100 khz mode 4.7 ? s time the bus must be free before a new transmission can start 400 khz mode 1.3 ? s d102 c b bus capacitive loading ? 400 pf note 1: as a transmitter, the device must provide this internal minimum delay time to bridge the undefined region (min. 300 ns) of the falling edge of scl to avoid unintended generation of start or stop conditions. 2: a fast mode i 2 c bus device can be used in a standard mode i 2 c bus system, but the requirement t su : dat 250 ns must then be met. this will automatically be the case if the device does not stretch the low period of the scl signal. if such a device does stretch the low period of the scl signal, it must out- put the next data bit to the sda line. t r max. + t su : dat = 1000 + 250 = 1250 ns (according to the standard mode i 2 c bus specification) before the scl line is released.
? 2003 microchip technology inc. preliminary ds39616b-page 367 pic18f2331/2431/4331/4431 figure 25-17: ssp i 2 c bus start/stop bits timing waveforms table 25-17: ssp i 2 c bus start/stop bits requirements figure 25-18: ssp i 2 c bus data timing note: refer to figure 25-4 for load conditions. 91 93 scl sda start condition stop condition 90 92 param. no. symbol characteristic min max units conditions 90 t su : sta start condition 100 khz mode 2(t osc )(brg + 1) ? ns only relevant for repeated start condition setup time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? 91 t hd : sta start condition 100 khz mode 2(t osc )(brg + 1) ? ns after this period, the first clock pulse is generated hold time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? 92 t su : sto stop condition 100 khz mode 2(t osc )(brg + 1) ? ns setup time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? 93 t hd : sto stop condition 100 khz mode 2(t osc )(brg + 1) ? ns hold time 400 khz mode 2(t osc )(brg + 1) ? 1 mhz mode (1) 2(t osc )(brg + 1) ? note 1: maximum pin capacitance = 10 pf for all i 2 c pins. note: refer to figure 25-4 for load conditions. 90 91 92 100 101 103 106 107 109 109 110 102 scl sda in sda out
pic18f2331/2431/4331/4431 ds39616b-page 368 preliminary ? 2003 microchip technology inc. table 25-18: ssp i 2 c bus data requirements param. no. symbol characteristic min max units conditions 100 t high clock high time 100 khz mode 2(t osc )(brg + 1) ? ms 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 101 t low clock low time 100 khz mode 2(t osc )(brg + 1) ? ms 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 102 t r sda and scl rise time 100 khz mode ? 1000 ns c b is specified to be from 10 to 400 pf 400 khz mode 20 + 0.1 c b 300 ns 1 mhz mode (1) ? 300 ns 103 t f sda and scl fall time 100 khz mode ? 300 ns c b is specified to be from 10 to 400 pf 400 khz mode 20 + 0.1 c b 300 ns 1 mhz mode (1) ? 100 ns 90 t su : sta start condition setup time 100 khz mode 2(t osc )(brg + 1) ? ms only relevant for repeated start condition 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 91 t hd : sta start condition hold time 100 khz mode 2(t osc )(brg + 1) ? ms after this period, the first clock pulse is generated 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 106 t hd : dat data input hold time 100 khz mode 0 ? ns 400 khz mode 0 0.9 ms 1 mhz mode (1) tbd ? ns 107 t su : dat data input setup time 100 khz mode 250 ? ns (note 2) 400 khz mode 100 ? ns 1 mhz mode (1) tbd ? ns 92 t su : sto stop condition setup time 100 khz mode 2(t osc )(brg + 1) ? ms 400 khz mode 2(t osc )(brg + 1) ? ms 1 mhz mode (1) 2(t osc )(brg + 1) ? ms 109 t aa output valid from clock 100 khz mode ? 3500 ns 400 khz mode ? 1000 ns 1 mhz mode (1) ??ns 110 t buf bus free time 100 khz mode 4.7 ? ms time the bus must be free before a new transmission can start 400 khz mode 1.3 ? ms 1 mhz mode (1) tbd ? ms d102 c b bus capacitive loading ? 400 pf note 1: maximum pin capacitance = 10 pf for all i 2 c pins. 2: a fast mode i 2 c bus device can be used in a standard mode i 2 c bus system, but parameter #107 250 ns must then be met. this will automatically be the case if the device does not stretch the low period of the scl signal. if such a device does stretch the low period of the scl signal, it must output the next data bit to the sda line, parameter #102 + parameter #107 = 1000 + 250 = 1250 ns (for 100 khz mode) before the scl line is released.
? 2003 microchip technology inc. preliminary ds39616b-page 369 pic18f2331/2431/4331/4431 figure 25-19: usart synchronous transmission (master/slave) timing table 25-19: usart synchronous transmission requirements figure 25-20: usart synchronous receive (master/slave) timing table 25-20: usart synchronous receive requirements 121 121 120 122 rc6/tx/ck rc7/rx/dt pin pin note: refer to figure 25-4 for load conditions. param no. symbol characteristic min max units conditions 120 tckh2dtv sync xmit (master & slave) clock high to data out valid pic18fxx31 ? 40 ns pic18lfxx31 ? 100 ns 121 tckrf clock out rise time and fall time (master mode) pic18fxx31 ? 20 ns pic18lfxx31 ? 50 ns 122 tdtrf data out rise time and fall time pic18fxx31 ? 20 ns pic18lfxx31 ? 50 ns 125 126 rc6/tx/ck rc7/rx/dt pin pin note: refer to figure 25-4 for load conditions. param. no. symbol characteristic min max units conditions 125 tdtv2ckl sync rcv (master & slave) data hold before ck (dt hold time) 10 ? ns 126 tckl2dtl data hold after ck (dt hold time) 15 ? ns
pic18f2331/2431/4331/4431 ds39616b-page 370 preliminary ? 2003 microchip technology inc. table 25-21: a/d converter characteristics: pic18f2331/2431/4331/4431 (industrial) pic18lf2331/2431/4331/4431 (industrial) param no. symbol characteristic min typ max units conditions device supply av dd analog v dd supply v dd -0.3 ? v dd +0.3 v av ss analog v ss supply v ss -0.3 v ss +0.3 v i ad module current (during conversion) 500 250 a a v dd = 5v v dd = 2.5v iado module current off 1.0 a ac timing parameters a10 f thr throughput rate ? ? 200 75 ksps ksps v dd = 5v, single channel v dd < 3v, single channel a11 t ad a/d clock period 385 1000 20,000 20,000 ns v dd = 5v v dd = 3v a12 t rc a/d internal rc oscillator period 500 750 10000 1500 2250 20000 ns ns ns pic18f parts pic18lf parts a vdd < 3.0v a13 t cnv conversion time (1) 12 12 12 t ad a14 t acq acquisition time (2) 2 (2) t ad a16 t tc conversion start from external 1/4 t cy 1tcy reference inputs a20 v re f reference voltage for 10-bit resolution (v re f+ - v re f- ) 1.5 1.8 ? ? a vdd -a vss a vdd -a vss v v v dd 3v v dd < 3v a21 v re fh reference voltage high (av dd or v ref +) 1.5v ? av dd vv dd 3v a22 v re fl reference voltage low (av ss or v ref -) av ss ?v refh -1.5v v a23 i ref reference current 150a 75a v dd = 5v v dd = 2.5v analog input characteristics a26 v ain input voltage (3) av ss -0.3 ? av dd +0.3 v a30 z ain recommended impedance of analog voltage source ??2.5k ? a31 z chin analog channel input impedance ? 10.0 k ? v dd = 3.0 v dc performance a41 n r resolution 10 bits ? a42 e i l integral nonlinearity ? ? < 1lsbv dd 3.0v v refh 3.0v a43 e i l differential nonlinearity ? ? < 1lsbv dd 3.0v v refh 3.0v a45 e off offset error ? 0.5 < 1.5 lsb v dd 3.0v v refh 3.0v a46 e ga gain error ? 0.5 < 1.5 lsb v dd 3.0v v refh 3.0v a47 ? monotonicity (4) guaranteed ? v dd 3.0v v refh 3.0v note 1: conversion time does not include acquisition time. see section 20.0 ?10-bit high-speed analog-to-digital converter (a/d) module? for a full discussion of acquisition time requirements. 2: in sequential modes, tacq should be 12tad or greater. 3: for v dd < 2.7v and temperature below 0c, v ain should be limited to range < v dd /2. 4: the a/d conversion result never dec reases with an incraese in the i nput voltage, and has no missing codes.
? 2003 microchip technology inc. preliminary ds39616b-page 371 pic18f2331/2431/4331/4431 26.0 preliminary dc and ac characteristics graphs and tables graphs are not available at this time.
pic18f2331/2431/4331/4431 ds39616b-page 372 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 373 pic18f2331/2431/4331/4431 27.0 packaging information 27.1 package marking information legend: xx...x customer specific information* y year code (last digit of calendar year) yy year code (last 2 digits of calendar year) ww week code (week of january 1 is week ?01?) nnn alphanumeric traceability code note : in the event the full microchip part number cannot be marked on one line, it will be carried over to the next line thus limiting the number of available characters for customer specific information. * standard picmicro device marking consists of microchip part number, year code, week code, and traceability code. for picmicro device marking beyond this, certain price adders apply. please check with your microchip sales office. for qtp devices, any special marking adders are included in qtp price. xxxxxxxxxxxxxxxxx yywwnnn 28-lead pdip (skinny dip) example xxxxxxxxxxxxxxxxx 0317017 pic18f2331-i/sp 28-lead soic yywwnnn example xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxx 0310017 PIC18F2431-e/so xxxxxxxxxxxxxxxxxx yywwnnn 40-lead pdip example xxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx pic18f4331-i/p 0312017 44-lead tqfp example xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx yywwnnn pic18f4431 -i/pt 0320017 44-lead qfn example xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx yywwnnn pic18f4431 -i/ml 0320017
pic18f2331/2431/4331/4431 ds39616b-page 374 preliminary ? 2003 microchip technology inc. 27.2 package details the following sections give the technical details of the packages. 28-lead skinny plastic dual in-line (sp) ? 300 mil (pdip) 15 10 5 15 10 5 mold draft angle bottom 15 10 5 15 10 5 mold draft angle top 10.92 8.89 8.13 .430 .350 .320 eb overall row spacing 0.56 0.48 0.41 .022 .019 .016 b lower lead width 1.65 1.33 1.02 .065 .053 .040 b1 upper lead width 0.38 0.29 0.20 .015 .012 .008 c lead thickness 3.43 3.30 3.18 .135 .130 .125 l tip to seating plane 35.18 34.67 34.16 1.385 1.365 1.345 d overall length 7.49 7.24 6.99 .295 .285 .275 e1 molded package width 8.26 7.87 7.62 .325 .310 .300 e shoulder to shoulder width 0.38 .015 a1 base to seating plane 3.43 3.30 3.18 .135 .130 .125 a2 molded package thickness 4.06 3.81 3.56 .160 .150 .140 a top to seating plane 2.54 .100 p pitch 28 28 n number of pins max nom min max nom min dimension limits millimeters inches* units 2 1 d n e1 c eb e p l a2 b b1 a a1 notes: jedec equivalent: mo-095 drawing no. c04-070 * controlling parameter dimension d and e1 do not include mold flash or protrusions. mold flash or protrusions shall not exceed .010? (0.254mm) per side. significant characteristic
? 2003 microchip technology inc. preliminary ds39616b-page 375 pic18f2331/2431/4331/4431 28-lead plastic small outline (so) ? wide, 300 mil (soic) foot angle top 048048 15 12 0 15 12 0 mold draft angle bottom 15 12 0 15 12 0 mold draft angle top 0.51 0.42 0.36 .020 .017 .014 b lead width 0.33 0.28 0.23 .013 .011 .009 c lead thickness 1.27 0.84 0.41 .050 .033 .016 l foot length 0.74 0.50 0.25 .029 .020 .010 h chamfer distance 18.08 17.87 17.65 .712 .704 .695 d overall length 7.59 7.49 7.32 .299 .295 .288 e1 molded package width 10.67 10.34 10.01 .420 .407 .394 e overall width 0.30 0.20 0.10 .012 .008 .004 a1 standoff 2.39 2.31 2.24 .094 .091 .088 a2 molded package thickness 2.64 2.50 2.36 .104 .099 .093 a overall height 1.27 .050 p pitch 28 28 n number of pins max nom min max nom min dimension limits millimeters inches* units 2 1 d p n b e e1 l c 45 h a2 a a1 * controlling parameter notes: dimensions d and e1 do not include mold flash or protrusions. mold flash or protrusions shall not exceed .010? (0.254mm) per side. jedec equivalent: ms-013 drawing no. c04-052 significant characteristic
pic18f2331/2431/4331/4431 ds39616b-page 376 preliminary ? 2003 microchip technology inc. 40-lead plastic dual in-line (p) ? 600 mil (pdip) 15 10 5 15 10 5 mold draft angle bottom 15 10 5 15 10 5 mold draft angle top 17.27 16.51 15.75 .680 .650 .620 eb overall row spacing 0.56 0.46 0.36 .022 .018 .014 b lower lead width 1.78 1.27 0.76 .070 .050 .030 b1 upper lead width 0.38 0.29 0.20 .015 .012 .008 c lead thickness 3.43 3.30 3.05 .135 .130 .120 l tip to seating plane 52.45 52.26 51.94 2.065 2.058 2.045 d overall length 14.22 13.84 13.46 .560 .545 .530 e1 molded package width 15.88 15.24 15.11 .625 .600 .595 e shoulder to shoulder width 0.38 .015 a1 base to seating plane 4.06 3.81 3.56 .160 .150 .140 a2 molded package thickness 4.83 4.45 4.06 .190 .175 .160 a top to seating plane 2.54 .100 p pitch 40 40 n number of pins max nom min max nom min dimension limits millimeters inches* units a2 1 2 d n e1 c eb e p l b b1 a a1 * controlling parameter notes: dimensions d and e1 do not include mold flash or protrusions. mold flash or protrusions shall not exceed .010? (0.254mm) per side. jedec equivalent: mo-011 drawing no. c04-016 significant characteristic
? 2003 microchip technology inc. preliminary ds39616b-page 377 pic18f2331/2431/4331/4431 44-lead plastic thin quad flatpack (pt) 10x10x1 mm body, 1.0/0.10 mm lead form (tqfp) * controlling parameter notes: dimensions d1 and e1 do not include mold flash or protrusions. mold flash or protrusions shall not exceed .010? (0.254mm) per side. jedec equivalent: ms-026 drawing no. c04-076 1.14 0.89 0.64 .045 .035 .025 ch pin 1 corner chamfer 1.00 .039 (f) footprint (reference) (f) a a1 a2 e e1 #leads=n1 p b d1 d n 1 2 c l units inches millimeters* dimension limits min nom max min nom max number of pins n 44 44 pitch p .031 0.80 overall height a .039 .043 .047 1.00 1.10 1.20 molded package thickness a2 .037 .039 .041 0.95 1.00 1.05 standoff a1 .002 .004 .006 0.05 0.10 0.15 foot length l .018 .024 .030 0.45 0.60 0.75 foot angle 03.5 7 03.5 7 overall width e .463 .472 .482 11.75 12.00 12.25 overall length d .463 .472 .482 11.75 12.00 12.25 molded package width e1 .390 .394 .398 9.90 10.00 10.10 molded package length d1 .390 .394 .398 9.90 10.00 10.10 pins per side n1 11 11 lead thickness c .004 .006 .008 0.09 0.15 0.20 lead width b .012 .015 .017 0.30 0.38 0.44 mold draft angle top 51015 51015 mold draft angle bottom 51015 51015 ch x 45 significant characteristic
pic18f2331/2431/4331/4431 ds39616b-page 378 preliminary ? 2003 microchip technology inc. 44-lead plastic quad flat no lead package (ml) 8x8 mm body (qfn) lead width *controlling parameter drawing no. c04-103 notes: dimensions d and e1 do not include mold flash or protrusions. mold flash or protrusions shall not exceed .010" (0.254mm) per side. b .012 .013 .013 0.30 0.33 0.35 pitch number of pins overall width standoff overall length overall height max units dimension limits a1 d e n p a .315 bsc .000 inches .026 bsc min 44 nom max .002 0 8.00 bsc millimeters* .039 min 44 0.65 bsc nom 0.05 1.00 .010 ref base thickness a3 0.25 ref jedec equivalent: m0-220 0.90 .035 .001 0.02 .315 bsc 8.00 bsc lead length l .014 .016 .018 0.35 0.40 0.45 e2 d2 exposed pad width exposed pad length .262 .268 .274 6.65 6.80 6.95 .262 .268 .274 6.65 6.80 6.95 d2 d a1 a3 a top view n 1 l e2 bottom view b e 2 pad metal exposed p pin 1 index on exposed pad top marking index on optional pin 1 .031 0.80
? 2003 microchip technology inc. preliminary ds39616b-page 379 pic18f2331/2431/4331/4431 appendix a: revision history revision a (june 2003) original data sheet for pic18f2331/2431/4331/4431 devices. revision b (december 2003) the electrical specifications in section 25.0 ?electri- cal characteristics? have been updated and there have been minor corrections to the data sheet text. appendix b: device differences the differences between the devices listed in this data sheet are shown in table b-1. table b-1: device differences features pic18f2331 PIC18F2431 pic18f4331 pic18f4431 program memory (bytes) 4096 8192 4096 8192 program memory (instructions) 2048 4096 2048 4096 interrupt sources 22 22 34 34 i/o ports ports a, b, c, d, e ports a, b, c, d, e ports a, b, c, d, e ports a, b, c, d, e capture/compare/pwm modules 2 2 2 2 enhanced capture/compare/ pwm modules 1111 parallel communications (psp) no no yes yes 10-bit analog-to-digital module 5 input channels 5 input channels 9 input channels 9 input channels packages 28-pin sdip 28-pin soic 28-pin sdip 28-pin soic 40-pin dip 44-pin tqfp 44-pin qfn 40-pin dip 44-pin tqfp 44-pin qfn
pic18f2331/2431/4331/4431 ds39616b-page 380 preliminary ? 2003 microchip technology inc. appendix c: conversion considerations this appendix discusses the considerations for converting from previous versions of a device to the ones listed in this data sheet. typically, these changes are due to the differences in the process technology used. an example of this type of conversion is from a pic16c74a to a pic16c74b. not applicable appendix d: migration from baseline to enhanced devices this section discusses how to migrate from a baseline device (i.e., pic16c5x) to an enhanced mcu device (i.e., pic18fxxx). the following are the list of modifications over the pic16c5x microcontroller family: not currently available
? 2003 microchip technology inc. preliminary ds39616b-page 381 pic18f2331/2431/4331/4431 appendix e: migration from mid-range to enhanced devices a detailed discussion of the differences between the mid-range mcu devices (i.e., pic16cxxx) and the enhanced devices (i.e., pic18fxxx) is provided in an716, ?migrating designs from pic16c74a/74b to pic18f442.? the changes discussed, while device specific, are generally applicable to all mid-range to enhanced device migrations. this application note is available on microchip?s web site; www.microchip.com. appendix f: migration from high-end to enhanced devices a detailed discussion of the migration pathway and differences between the high-end mcu devices (i.e., pic17cxxx) and the enhanced devices (i.e., pic18fxxx) is provided in an726, ?pic17cxxx to pic18fxxx migration.? this application note is available on microchip?s web site; www.microchip.com.
pic18f2331/2431/4331/4431 ds39616b-page 382 preliminary ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. ds39616b-page 383 pic18f2331/2431/4331/4431 index a a/d ................................................................................... 243 associated registers ............................................... 259 calculating the minimum required acquisition time ............................................... 254 special event trigger (ccp) .................................... 154 absolute maximum ratings ............................................. 337 ac (timing) characteristics ............................................. 353 load conditions for device timing specifications ....................................... 354 parameter symbology ............................................. 353 temperature and voltage specifications ................. 354 timing conditions .................................................... 354 access bank ...................................................................... 70 ack pulse ................................................................ 217 , 218 addlw ............................................................................ 293 addwf ............................................................................ 293 addwfc ......................................................................... 294 analog-to-digital converter. see a/d. andlw ............................................................................ 294 andwf ............................................................................ 295 application notes an578 (use of the ssp module in the i 2 c multi-master environment) ............................... 211 assembler mpasm assembler .................................................. 331 auto-wake-up on sync break character ......................... 235 b bank select register (bsr) ............................................... 70 bc .................................................................................... 295 bcf .................................................................................. 296 bf bit ................................................................................ 212 block diagrams analog input model .................................................. 254 capture mode operation ......................................... 153 compare mode operation ....................................... 154 external power-on reset circuit (slow v dd power-up) ......................................... 46 fail-safe clock monitor ............................................ 281 generic i/o port ....................................................... 107 interrupt logic ............................................................ 92 low-voltage detect (lvd) ....................................... 262 low-voltage detect (lvd) with external input ......... 262 on-chip reset circuit ................................................ 45 pic18f2331/2431 ...................................................... 10 pic18f4331/4431 ...................................................... 11 pll ............................................................................. 22 pwm (standard) ...................................................... 156 ra0 pin .................................................................... 108 ra1 pin .................................................................... 108 ra3:ra2 pins .......................................................... 108 ra4 pin .................................................................... 109 ra5 pin .................................................................... 110 ra6 pin .................................................................... 110 rb3:rb0 pins .......................................................... 113 rb4 pin .................................................................... 114 rb5 pin ............................................................ 115 , 121 rb7:rb6 pins .......................................................... 116 rc0 pin .................................................................... 118 rc1 pin .................................................................... 119 rc2 pin .................................................................... 119 rc3 pin ................................................................... 120 rc4 pin ................................................................... 120 rc6 pin ................................................................... 121 rc7 pin ................................................................... 122 rd0 pin ................................................................... 127 rd1 pin ................................................................... 127 rd2 pin ................................................................... 126 rd3 pin ................................................................... 126 rd4 pin ................................................................... 125 rd5 pin ................................................................... 125 rd7:rd6 pins ......................................................... 124 re2:re0 pins .......................................................... 130 re3 pin ................................................................... 130 reads from flash program memory .......................... 79 ssp (i 2 c mode) ....................................................... 217 ssp (spi mode) ...................................................... 214 system clock ............................................................. 27 table read operation ............................................... 75 table write operation ................................................ 76 table writes to flash program memory .................... 81 timer0 in 16-bit mode .............................................. 134 timer0 in 8-bit mode ................................................ 134 timer1 ..................................................................... 138 timer1 (16-bit read/write mode) ............................ 138 timer2 ..................................................................... 144 timer5 ..................................................................... 146 usart receive ....................................................... 233 usart transmit ...................................................... 231 watchdog timer ...................................................... 278 bn .................................................................................... 296 bnc ................................................................................. 297 bnn ................................................................................. 297 bnov ............................................................................... 298 bnz .................................................................................. 298 bor. see brown-out reset. bov ................................................................................. 301 bra ................................................................................. 299 break character (12-bit) transmit and receive .............. 236 brown-out reset (bor) ..............................................46 , 267 bsf .................................................................................. 299 btfsc ............................................................................. 300 btfss ............................................................................. 300 btg ................................................................................. 301 bz .................................................................................... 302 c c compilers mplab c17 ............................................................. 332 mplab c18 ............................................................. 332 mplab c30 ............................................................. 332 call ................................................................................ 302 capture (ccp module) .................................................... 153 associated registers ............................................... 155 ccp pin configuration ............................................. 153 ccpr1h:ccpr1l registers ................................... 153 software interrupt .................................................... 153 timer1 mode selection ............................................ 153 capture/compare/pwm (ccp) ....................................... 151 capture mode. see capture. ccp1 ....................................................................... 152 ccpr1h register ........................................... 152 ccpr1l register ............................................ 152
pic18f2331/2431/4331/4431 ds39616b-page 384 ? 2003 microchip technology inc. ccp2 ........................................................................ 152 ccpr2h register ............................................ 152 ccpr2l register ............................................ 152 compare mode. see compare. pwm mode. see pwm. timer resources ...................................................... 152 cke bit ............................................................................. 212 ckp bit ............................................................................. 213 clock sources .................................................................... 26 selection using osccon register ........................... 26 clocking scheme/instruction cycle .................................... 61 clrf ................................................................................ 303 clrwdt .......................................................................... 303 code examples 16 x 16 signed multiply routine ................................. 90 16 x 16 unsigned multiply routine ............................. 90 8 x 8 signed multiply routine ..................................... 89 8 x 8 unsigned multiply routine ................................. 89 changing between capture prescalers ................... 153 computed goto using an offset value ................... 63 data eeprom read ................................................. 87 data eeprom refresh routine ................................ 88 data eeprom write .................................................. 87 erasing a flash program memory row ..................... 80 fast register stack .................................................... 60 how to clear ram (bank 1) using indirect addressing ......................................................... 71 implementing a real-time clock using a timer1 interrupt service .................................. 141 initializing porta .................................................... 107 initializing portb .................................................... 112 initializing portc .................................................... 118 initializing portd .................................................... 124 initializing porte .................................................... 129 reading a flash program memory word ................... 79 saving status, wreg and bsr registers in ram ..................................... 106 writing to flash program memory ....................... 82 ? 83 code protection ....................................................... 267 , 283 comf ............................................................................... 304 compare (ccp module) ................................................... 154 associated registers ............................................... 155 ccp pin configuration ............................................. 154 ccpr1 register ....................................................... 154 software interrupt ..................................................... 154 special event trigger ............................................... 154 timer1 mode selection ............................................ 154 computed goto ............................................................... 63 configuration bits ............................................................. 267 configuration register protection .................................... 286 context saving during interrupts ..................................... 106 control registers eecon1 and eecon2 .............................................. 76 conversion considerations .............................................. 380 cpfseq .......................................................................... 304 cpfsgt ........................................................................... 305 cpfslt ........................................................................... 305 crystal oscillator/ceramic resonator ................................ 21 d d/a bit .............................................................................. 212 data eeprom code protection ...................................... 286 data eeprom memory ..................................................... 85 associated registers ................................................. 88 eeadr register ........................................................ 85 eecon1 and eecon2 registers ............................. 85 operation during code-protect ................................. 88 protection against spurious write ............................. 87 reading ..................................................................... 87 using .......................................................................... 88 write verify ................................................................ 87 writing ........................................................................ 87 data memory ..................................................................... 63 general purpose registers ....................................... 63 map for pic18f2x31/4x31 ........................................ 64 special function registers ........................................ 65 data/address bit (d/a ) ..................................................... 212 daw ................................................................................ 306 dc and ac characteristics graphs and tables (preliminary) ............................. 371 dc characteristics ............................................339 , 340 , 349 dcfsnz .......................................................................... 307 decf ............................................................................... 306 decfsz .......................................................................... 307 demonstration boards picdem 1 ................................................................ 334 picdem 17 .............................................................. 334 picdem 18r pic18c601/801 ................................. 335 picdem 2 plus ........................................................ 334 picdem 3 pic16c92x ............................................ 334 picdem 4 ................................................................ 334 picdem lin pic16c43x ........................................ 335 picdem usb pic16c7x5 ...................................... 335 picdem.net internet/ethernet ................................. 334 development support ...................................................... 331 device differences ........................................................... 379 device overview .................................................................. 7 features (table) ........................................................... 9 new core features ...................................................... 7 other special features ................................................ 8 direct addressing ............................................................... 72 e effects of power managed modes on various clock sources ............................................................ 29 electrical characteristics .................................................. 337 enhanced universal synchronous asynchronous receiver transmitter (usart) ................................ 221 equations 16 x 16 signed multiplication algorithm ..................... 90 16 x 16 unsigned multiplication algorithm ................. 90 a/d acquisition time ............................................... 253 a/d minimum charging time ................................... 253 errata ................................................................................... 6 evaluation and programming tools ................................. 335 external clock input ........................................................... 23 f fail-safe clock monitor .............................................267 , 281 interrupts in power-managed modes ....................... 282 por or wake from sleep ........................................ 282 wdt during oscillator failure ................................. 281 fast register stack ............................................................ 60 firmware instructions ....................................................... 287 flash program memory ..................................................... 75 associated registers ................................................. 83 control registers ....................................................... 76 erase sequence ........................................................ 80 erasing ....................................................................... 80 operation during code-protect ................................. 83 reading ..................................................................... 79 tablat register ....................................................... 78
? 2003 microchip technology inc. ds39616b-page 385 pic18f2331/2431/4331/4431 table pointer .............................................................. 78 boundaries based on operation ........................ 78 table pointer boundaries .......................................... 78 table reads and table writes .................................. 75 unexpected termination of write operation .............. 83 write verify ................................................................ 83 writing to .................................................................... 81 fscm. see fail-safe clock monitor. g goto ............................................................................... 308 h hardware multiplier ............................................................ 89 introduction ................................................................ 89 operation ................................................................... 89 performance comparison .......................................... 89 hspll ................................................................................ 22 i i/o ports ........................................................................... 107 i 2 c mode addressing ............................................................... 218 associated registers ............................................... 220 master mode ............................................................ 220 mode selection ........................................................ 217 multi-master mode ................................................... 220 operation ................................................................. 217 reception ................................................................. 218 slave mode scl and sda pins ........................................... 217 transmission ............................................................ 219 id locations ............................................................. 267 , 286 incf ................................................................................. 308 incfsz ............................................................................ 309 in-circuit debugger .......................................................... 286 in-circuit serial programming (icsp) ...................... 267 , 286 indirect addressing indf and fsr registers ........................................... 71 operation ................................................................... 71 indirect addressing operation ............................................ 72 indirect file operand .......................................................... 63 infsnz ............................................................................ 309 initialization conditions for all registers ...................... 48 ? 51 instruction cycle ................................................................. 61 instruction flow/pipelining ................................................. 61 instruction format ............................................................ 289 instruction set .................................................................. 287 addlw .................................................................... 293 addwf .................................................................... 293 addwfc ................................................................. 294 andlw .................................................................... 294 andwf .................................................................... 295 bc ............................................................................ 295 bcf .......................................................................... 296 bn ............................................................................ 296 bnc ......................................................................... 297 bnn ......................................................................... 297 bnov ....................................................................... 298 bnz .......................................................................... 298 bov ......................................................................... 301 bra .......................................................................... 299 bsf .......................................................................... 299 btfsc ..................................................................... 300 btfss ..................................................................... 300 btg .......................................................................... 301 bz ............................................................................ 302 call ........................................................................ 302 clrf ....................................................................... 303 clrwdt ................................................................. 303 comf ...................................................................... 304 cpfseq .................................................................. 304 cpfsgt .................................................................. 305 cpfslt ................................................................... 305 daw ........................................................................ 306 dcfsnz .................................................................. 307 decf ....................................................................... 306 decfsz .................................................................. 307 goto ...................................................................... 308 incf ........................................................................ 308 incfsz .................................................................... 309 infsnz .................................................................... 309 iorlw ..................................................................... 310 iorwf ..................................................................... 310 lfsr ....................................................................... 311 movf ...................................................................... 311 movff .................................................................... 312 movlb .................................................................... 312 movlw ................................................................... 313 movwf ................................................................... 313 mullw .................................................................... 314 mulwf .................................................................... 314 negf ....................................................................... 315 nop ......................................................................... 315 pop ......................................................................... 316 push ....................................................................... 316 rcall ..................................................................... 317 reset ..................................................................... 317 retfie .................................................................... 318 retlw .................................................................... 318 return .................................................................. 319 rlcf ....................................................................... 319 rlncf ..................................................................... 320 rrcf ....................................................................... 320 rrncf .................................................................... 321 setf ....................................................................... 321 sleep ..................................................................... 322 subfwb ................................................................. 322 sublw .................................................................... 323 subwf .................................................................... 323 subwfb ................................................................. 324 swapf .................................................................... 325 tblrd ..................................................................... 326 tblwt .................................................................... 327 tstfsz ................................................................... 328 xorlw ................................................................... 328 xorwf ................................................................... 329 summary table ....................................................... 290 instructions in program memory ........................................ 62 two-word instructions ............................................... 62 intcon register rbif bit ................................................................... 112 intcon registers ............................................................. 93 inter-integrated circuit (i 2 c). see i 2 c mode. internal oscillator block ..................................................... 24 adjustment ................................................................. 24 intio modes ............................................................. 24 intrc output frequency .......................................... 24 osctune register ................................................... 24 internal rc oscillator use with wdt .......................................................... 278
pic18f2331/2431/4331/4431 ds39616b-page 386 ? 2003 microchip technology inc. interrupt sources .............................................................. 267 capture complete (ccp) ......................................... 153 compare complete (ccp) ....................................... 154 interrupt-on-change (rb7:rb4) .............................. 112 intn pin ................................................................... 106 portb, interrupt-on-change .................................. 106 tmr0 ....................................................................... 106 tmr1 overflow ........................................................ 137 tmr2 to pr2 match ................................................. 144 tmr2 to pr2 match (pwm) ............................ 143 , 156 interrupts ............................................................................ 91 interrupts, enable bits ccp1 enable (ccp1ie bit) ...................................... 153 interrupts, flag bits ccp1 flag (ccp1if bit) .......................................... 153 ccp1if flag (ccp1if bit) ....................................... 154 interrupt-on-change (rb7:rb4) flag (rbif bit) ...... 112 intosc frequency drift .................................................... 42 intosc, intrc. see internal oscillator block. iorlw ............................................................................. 310 iorwf ............................................................................. 310 ipr registers ................................................................... 102 l lfsr ................................................................................ 311 look-up tables .................................................................. 63 low-voltage detect .......................................................... 261 low-voltage detect characteristics ......................................................... 352 effects of a reset ..................................................... 265 operation ................................................................. 264 current consumption ....................................... 265 reference voltage set point ............................ 265 operation during sleep ............................................ 265 low-voltage icsp programming ..................................... 286 lvd. see low-voltage detect. m memory organization ......................................................... 57 data memory .............................................................. 63 program memory ....................................................... 57 memory programming requirements .............................. 351 migration from baseline to enhanced devices ................ 380 migration from high-end to enhanced devices ............... 381 migration from mid-range to enhanced devices ............. 381 movf ............................................................................... 311 movff ............................................................................. 312 movlb ............................................................................. 312 movlw ............................................................................ 313 movwf ........................................................................... 313 mplab asm30 assembler, linker, librarian .................. 332 mplab icd 2 in-circuit debugger ................................... 333 mplab ice 2000 high performance universal in-circuit emulator ................................................... 333 mplab ice 4000 high performance universal i n-circuit emulator .................................................... 333 mplab integrated development environment software .............................................. 331 mplink object linker/mplib object librarian ............... 332 mullw ............................................................................ 314 mulwf ............................................................................ 314 n negf ............................................................................... 315 nop ................................................................................. 315 o opcode field descriptions ............................................... 288 option_reg register psa bit .................................................................... 135 t0cs bit .................................................................. 135 t0ps2:t0ps0 bits ................................................... 135 t0se bit ................................................................... 135 oscillator configuration ...................................................... 21 ec .............................................................................. 21 ecio .......................................................................... 21 hs .............................................................................. 21 hspll ....................................................................... 21 internal oscillator block ............................................. 24 intio1 ....................................................................... 21 intio2 ....................................................................... 21 lp .............................................................................. 21 rc .............................................................................. 21 rcio .......................................................................... 21 xt .............................................................................. 21 oscillator selection .......................................................... 267 oscillator start-up timer (ost) ....................................29 , 46 oscillator switching ............................................................ 26 oscillator transitions ......................................................... 28 oscillator, timer1 ............................................................. 137 p p (stop) bit ....................................................................... 212 packaging information ..................................................... 373 marking .................................................................... 373 pickit 1 flash starter kit .................................................. 335 picstart plus development programmer .................... 333 pie registers ..................................................................... 99 pin functions mclr /v pp /re3 ....................................................12 , 15 osc1/clki/ra7 ...................................................12 , 15 osc2/clko/ra6 .................................................12 , 15 ra0/an0 ...............................................................12 , 15 ra1/an1 ...............................................................12 , 15 ra2/an2/v ref -/cap1/indx ................................12 , 15 ra3/an3/v ref +/cap2/qea .................................12 , 15 ra4/an4/cap3/qeb ................................................. 15 ra4/cap3/qeb ......................................................... 12 ra5/an5/lvdin ........................................................ 15 rb0/pwm0 ...........................................................13 , 16 rb1/pwm1 ...........................................................13 , 16 rb2/pwm2 ...........................................................13 , 16 rb3/pwm3 ...........................................................13 , 16 rb4/kbi0/pwm5 ....................................................... 16 rb4/pwm5 ................................................................ 13 rb5/kbi1/pwm4/pgm .........................................13 , 16 rb6/kbi2/pgc .....................................................13 , 16 rb7/kbi3/pgd .....................................................13 , 16 rc0/t1oso/t1cki ..............................................14 , 17 rc1/t1osi/ccp2/flta .......................................14 , 17 rc2/ccp1/fltb ..................................................14 , 17 rc3/t0cki/t5cki/int0 .......................................14 , 17 rc4/int1/sdi/sda ..............................................14 , 17 rc5/int2/sck/scl .............................................14 , 17 rc6/tx/ck/ss .....................................................14 , 17 rc7/rx/dt/sdo ..................................................14 , 17 rd0/t0cki/t5cki ..................................................... 18 rd1/sdo ................................................................... 18 rd2/sdi/sda ............................................................ 18 rd3/sck/scl ........................................................... 18 rd4/flta .................................................................. 18
? 2003 microchip technology inc. ds39616b-page 387 pic18f2331/2431/4331/4431 rd5/pwm4 ................................................................ 18 rd6/pwm6 ................................................................ 18 rd7/pwm7 ................................................................ 18 re0/an6 .................................................................... 19 re1/an7 .................................................................... 19 re2/an8 .................................................................... 19 v dd ....................................................................... 14 , 19 v ss ....................................................................... 14 , 19 pinout i/o descriptions pic18f2331/2431 ...................................................... 12 pic18f4331/4431 ...................................................... 15 pir registers ..................................................................... 96 pll lock time-out ............................................................. 46 pointer, fsrn ..................................................................... 71 pop .................................................................................. 316 por. see power-on reset. porta associated registers ............................................... 111 lata register .......................................................... 107 porta register ...................................................... 107 trisa register ........................................................ 107 portb associated registers ............................................... 117 latb register .......................................................... 112 portb register ...................................................... 112 rb7:rb4 interrupt-on-change flag (rbif bit) ........ 112 trisb register ........................................................ 112 portc associated registers ............................................... 123 latc register ......................................................... 118 portc register ...................................................... 118 trisc register ........................................................ 118 portd associated registers ............................................... 128 latd register ......................................................... 124 portd register ...................................................... 124 trisd register ........................................................ 124 porte associated registers ............................................... 132 late register .......................................................... 129 porte register ...................................................... 129 trise register ........................................................ 129 postscaler, wdt assignment (psa bit) .............................................. 135 rate select (t0ps2:t0ps0 bits) ............................. 135 power-managed modes ..................................................... 31 entering ...................................................................... 32 idle modes ................................................................. 33 run modes ................................................................. 38 selecting .................................................................... 31 sleep mode ................................................................ 33 summary (table) ........................................................ 31 wake from .................................................................. 40 power-on reset (por) .............................................. 46 , 267 oscillator start-up timer (ost) ......................... 46 , 267 power-up timer (pwrt) ................................... 46 , 267 time-out sequence .................................................... 46 power-up delays ................................................................ 29 power-up timer (pwrt) .............................................. 29 , 46 prescaler, capture ........................................................... 153 prescaler, timer0 ............................................................. 135 assignment (psa bit) .............................................. 135 rate select (t0ps2:t0ps0 bits) ............................. 135 prescaler, timer2 ............................................................. 157 pro mate ii universal device programmer .................. 333 program counter pcl register ............................................................. 60 pclath register ...................................................... 60 pclatu register ...................................................... 60 program memory interrupt vector .......................................................... 57 map and stack pic18f2331/4331 ............................................. 57 PIC18F2431/4431 ............................................. 57 reset vector .............................................................. 57 program memory code protection .................................. 284 program verification ........................................................ 283 program verification and code protection associated registers ............................................... 283 programming, device instructions ................................... 287 pulse width modulation. see pwm (ccp module) and pwm (eccp module). push ............................................................................... 316 push and pop instructions .............................................. 59 pwm (ccp module) ........................................................ 156 associated registers ............................................... 157 ccpr1h:ccpr1l registers ................................... 156 duty cycle ............................................................... 156 example frequencies/resolutions .......................... 157 period ...................................................................... 156 set-up for pwm operation ...................................... 157 tmr2 to pr2 match .........................................143 , 156 q q clock ............................................................................ 157 qei sampling modes ....................................................... 172 r r/w bit .............................................................. 212 , 218 , 219 ram. see data memory. rc oscillator ...................................................................... 23 rcio oscillator mode ................................................ 23 rcall ............................................................................. 317 rcon register bit status during initialization .................................... 47 bits and positions ...................................................... 47 rcsta register spen bit .................................................................. 221 receive overflow indicator bit (sspov) ......................... 213 register file ....................................................................... 63 registers baudctl (baud rate control) ............................... 224 ccpxcon (capture/compare/pwm control) ......... 151 config1h (configuration 1 high) .......................... 268 config2h (configuration 2 high) ...................270 , 271 config2l (configuration 2 low) ........................... 269 config3h (configuration 3 high) .......................... 272 config4l (configuration 4 low) ........................... 273 config5h (configuration 5 high) .......................... 274 config6h (configuration 6 high) .......................... 275 config6l (configuration 6 low) ........................... 275 config7h (configuration 7 high) .......................... 276 config7l (configuration 7 low) ........................... 276 device id register 1 ................................................ 277 device id register 2 ................................................ 277 eecon1 (data eeprom control 1) ....................77 , 86 intcon (interrupt control) ........................................ 93 intcon2 (interrupt control 2) ................................... 94 intcon3 (interrupt control 3) ................................... 95 ipr1 (peripheral interrupt priority 1) ....................... 102 ipr2 (peripheral interrupt priority 2) ....................... 103
pic18f2331/2431/4331/4431 ds39616b-page 388 ? 2003 microchip technology inc. lvdcon (lvd control) ........................................... 263 osccon (oscillator control) .................................... 28 osctune (oscillator tuning) ................................... 25 pie1 (peripheral interrupt enable 1) .......................... 99 pie2 (peripheral interrupt enable 2) ........................ 100 pir1 (peripheral interrupt request (flag) 1) ............. 96 pir2 (peripheral interrupt request (flag) 2) ............. 97 rcon (reset control) ....................................... 74 , 105 rcsta (receive status and control) ...................... 223 sspcon (sync serial po rt control) register .......... 213 sspstat (sync serial port status) register .......... 212 status ......................................................................... 73 stkptr (stack pointer) ............................................ 59 summary .............................................................. 66 ? 68 t0con (timer0 control) .......................................... 133 t1con (timer 1 control) ......................................... 137 t2con (timer 2 control) ......................................... 143 trise ....................................................................... 131 txsta (transmit status and control) ..................... 222 wdtcon (watchdog timer control) ....................... 278 reset .......................................................................... 45 , 317 resets .............................................................................. 267 retfie ............................................................................ 318 retlw ............................................................................. 318 return .......................................................................... 319 return address stack ........................................................ 58 return stack pointer (stkptr) ........................................ 58 revision history ............................................................... 379 rlcf ................................................................................ 319 rlncf ............................................................................. 320 rrcf ............................................................................... 320 rrncf ............................................................................. 321 s s (start) bit ....................................................................... 212 sck .................................................................................. 211 scl .................................................................................. 217 sdi ................................................................................... 211 sdo ................................................................................. 211 serial clock (sck) pin ..................................................... 211 serial data in (sdi) pin .................................................... 211 serial data out (sdo) pin ............................................... 211 setf ................................................................................ 321 slave select (ss ) pin ....................................................... 211 sleep ................................................................................ 322 osc1 and osc2 pin states ...................................... 29 smp bit ............................................................................. 212 software simulator (mplab sim) .................................... 332 software simulator (mplab sim30) ................................ 332 special event trigger. see compare (ccp module). special features of the cpu ............................................ 267 special function registers ................................................ 65 map ............................................................................ 65 spi mode ......................................................................... 211 associated registers ............................................... 216 serial clock .............................................................. 211 serial data in ........................................................... 211 serial data out ......................................................... 211 slave select ............................................................. 211 ss .................................................................................... 211 ssp overview tmr2 output for clock shift ............................ 143 , 144 ssp i 2 c operation ........................................................... 217 slave mode .............................................................. 217 sspen bit ........................................................................ 213 sspm<3:0> bits .............................................................. 213 sspov bit ....................................................................... 213 stack full/underflow resets .............................................. 59 subfwb ......................................................................... 322 sublw ............................................................................ 323 subwf ............................................................................ 323 subwfb ......................................................................... 324 swapf ............................................................................ 325 synchronous serial port enable bit (sspen) ................. 213 synchronous serial port mode select bits (sspm<3:0>) ........................................................... 213 synchronous serial port. see ssp. t tablat register ............................................................... 78 table pointer operations (table) ........................................ 78 table reads/table writes ................................................. 63 tblptr register ............................................................... 78 tblrd ............................................................................. 326 tblwt ............................................................................. 327 time-out in various situations (table) ................................ 47 timer0 .............................................................................. 133 16-bit mode timer reads and writes ...................... 135 associated registers ............................................... 135 clock source edge select (t0se bit) ..................... 135 clock source select (t0cs bit) ............................... 135 interrupt ................................................................... 135 operation ................................................................. 135 prescaler. see prescaler, timer0. switching prescaler assignment ............................. 135 timer1 .............................................................................. 137 16-bit read/write mode ........................................... 140 associated registers ............................................... 141 interrupt ................................................................... 140 operation ................................................................. 138 oscillator ...........................................................137 , 139 oscillator layout considerations ............................. 139 overflow interrupt .................................................... 137 resetting, using a special event trigger output (ccp) ................................................... 140 special event trigger (ccp) ................................... 154 tmr1h register ...................................................... 137 tmr1l register ....................................................... 137 use as a real-time clock ....................................... 140 timer2 .............................................................................. 143 associated registers ............................................... 144 operation ................................................................. 143 postscaler. see postscaler, timer2. pr2 register ....................................................143 , 156 prescaler. see prescaler, timer2. ssp clock shift ................................................143 , 144 tmr2 register ......................................................... 143 tmr2 to pr2 match interrupt ...................143 , 144 , 156 timer5 block diagram ......................................................... 146 timing diagrams asynchronous reception ......................................... 234 asynchronous transmission .................................... 231 asynchronous transmission (back to back) ........... 231 auto-wake-up bit (wue) during normal operation ............................................ 235 auto-wake-up bit (wue) during sleep ................... 235 brown-out reset (bor) ........................................... 358 capture/compare/pwm (ccp) ............................... 360 clko and i/o .......................................................... 357 clock, instruction cycle ............................................. 61
? 2003 microchip technology inc. ds39616b-page 389 pic18f2331/2431/4331/4431 example spi master mode (cke = 0) ..................... 361 example spi master mode (cke = 1) ..................... 362 example spi slave mode (cke = 0) ....................... 363 example spi slave mode (cke = 1) ....................... 364 external clock (all modes except pll) .................... 355 fail-safe clock monitor ............................................ 282 i 2 c bus data ............................................................ 365 i 2 c bus start/stop bits ............................................. 365 i 2 c reception (7-bit address) .................................. 219 i 2 c transmission (7-bit address) ............................. 219 low-voltage detect .................................................. 264 low-voltage detect characteristics ......................... 352 master ssp i 2 c bus data ........................................ 367 master ssp i 2 c bus start/stop bits ........................ 367 pwm output ............................................................ 156 reset, watchdog timer (wdt), oscillator start-up timer (ost), power-up timer (pwrt) ........... 358 send break character sequence ............................ 236 slow rise time (mclr tied to v dd , v dd rise > t pwrt ) ............................................ 55 spi mode (master mode) ......................................... 215 spi mode (slave mode with cke = 0) ..................... 215 spi mode (slave mode with cke = 1) ..................... 216 synchronous reception (master mode, sren) ...... 239 synchronous transmission ...................................... 237 synchronous transmission (through txen) .......... 238 time-out sequence on por w/pll enabled (mclr tied to v dd ) ........................................... 55 time-out sequence on power-up (mclr not tied to v dd ): case 1 .......................................... 54 time-out sequence on power-up (mclr not tied to v dd ): case 2 .......................................... 54 time-out sequence on power-up (mclr tied to v dd , v dd rise < t pwrt ) ........................ 54 timer0 and timer1 external clock .......................... 359 transition for entry to sec_idle mode .................... 36 transition for entry to sec_run mode .................... 38 transition for entry to sleep mode ............................ 34 transition for two-speed start-up (intosc to hspll) ......................................... 280 transition for wake from rc_run mode (rc_run to nfp) ............................................. 37 transition for wake from sec_run mode (secondary clock to hspll) ............................. 36 transition for wake from sleep (hspll) ................... 34 transition timing for wake from pri_idle mode ... 35 transition timing to pri_idle mode ........................ 35 transition to rc_idle mode ..................................... 37 transition to rc_run mode ..................................... 39 usart synchronous receive ( master/slave) ........ 369 usart synchronoustransmission (master/slave) .................................................. 369 timing diagrams and specifications ................................ 355 capture/compare/pwm requirements ................... 360 clko and i/o requirements ................................... 357 dc characteristics - internal rc accuracy .............. 356 example spi mode requirements (master mode, cke = 0) .................................. 361 example spi mode requirements (master mode, cke = 1) .................................. 362 example spi mode requirements (slave mode, cke = 0) .................................... 363 example spi slave mode requirements (cke = 1) ......................................................... 364 external clock requirements .................................. 355 i 2 c bus data requirements (slave mode) .............. 366 master ssp i 2 c bus data requirements ................ 368 master ssp i 2 c bus start/stop bits requirements .................................................. 367 pll clock ................................................................ 356 reset, watchdog timer, oscillator start-up timer, power-up timer and brown-out reset requirements ........................................ 358 timer0 and timer1 external clock requirements ... 359 usart synchronous receive requirements ......... 369 usart synchronous transmission requirements .................................................. 369 top-of-stack access .......................................................... 58 tstfsz ........................................................................... 328 two-speed start-up ..................................................267 , 279 two-word instructions example cases .......................................................... 62 txsta register brgh bit ................................................................. 225 u ua .................................................................................... 212 update address bit, ua ................................................... 212 usart asynchronous mode ................................................ 230 12-bit break transmit and receive ................. 236 associated registers, receive ........................ 234 associated registers, transmit ....................... 232 auto-wake-up on sync break ......................... 235 receiver .......................................................... 233 setting up 9-bit mode with address detect ..... 233 transmitter ...................................................... 230 baud rate generator (brg) ................................... 225 associated registers ....................................... 226 auto-baud rate detect .................................... 229 baud rate error, calculating ........................... 225 baud rates, asynchronous modes ................. 226 high baud rate select (brgh bit) ................. 225 power-managed mode operation ................... 225 sampling .......................................................... 225 serial port enable (spen bit) ................................. 221 synchronous master mode ...................................... 237 associated registers, reception ..................... 240 associated registers, transmit ....................... 238 reception ........................................................ 239 transmission ................................................... 237 synchronous slave mode ........................................ 241 associated registers, receive ........................ 242 associated registers, transmit ....................... 241 reception ........................................................ 242 transmission ................................................... 241 w watchdog timer (wdt) ............................................267 , 278 associated registers ............................................... 279 control register ....................................................... 278 during oscillator failure .......................................... 281 programming considerations .................................. 278 wcol bit ......................................................................... 213 write collision detect bit (wcol) ................................... 213 www, on-line support ...................................................... 6 x xorlw ............................................................................ 328 xorwf ........................................................................... 329
pic18f2331/2431/4331/4431 ds39616b-page 390 ? 2003 microchip technology inc. notes:
? 2003 microchip technology inc. preliminary ds39616b-page 391 pic18f2331/2431/4331/4431 on-line support microchip provides on-line support on the microchip world wide web site. the web site is used by microchip as a means to make files and information easily available to customers. to view the site, the user must have access to the internet and a web browser, such as netscape ? or microsoft ? internet explorer. files are also available for ftp download from our ftp site. connecting to the microchip internet web site the microchip web site is available at the following url: www.microchip.com the file transfer site is available by using an ftp ser- vice to connect to: ftp://ftp.microchip.com the web site and file transfer site provide a variety of services. users may download files for the latest development tools, data sheets, application notes, user's guides, articles and sample programs. a vari- ety of microchip specific business information is also available, including listings of microchip sales offices, distributors and factory representatives. other data available for consideration is:  latest microchip press releases  technical support section with frequently asked questions  design tips  device errata  job postings  microchip consultant program member listing  links to other useful web sites related to microchip products  conferences for products, development systems, technical information and more  listing of seminars and events systems information and upgrade hot line the systems information and upgrade line provides system users a listing of the latest versions of all of microchip's development systems software products. plus, this line provides information on how customers can receive the most current upgrade kits. the hot line numbers are: 1-800-755-2345 for u.s. and most of canada, and 1-480-792-7302 for the rest of the world. 042003
pic18f2331/2431/4331/4431 ds39616b-page 392 preliminary ? 2003 microchip technology inc. reader response it is our intention to provide you with the best documentation possible to ensure successful use of your microchip prod- uct. if you wish to provide your comments on organization, clarity, subject matter, and ways in which our documentation can better serve you, please fax your comments to the technical publications manager at (480) 792-4150. please list the following information, and use this outline to provide us with your comments about this document. to : technical publications manager re: reader response total pages sent ________ from: name company address city / state / zip / country telephone: (_______) _________ - _________ application (optional): would you like a reply? y n device: literature number: questions: fax: (______) _________ - _________ ds39616b pic18f2331/2431/4331/4431 1. what are the best features of this document? 2. how does this document meet your hardware and software development needs? 3. do you find the organization of this document easy to follow? if not, why? 4. what additions to the document do you think would enhance the structure and subject? 5. what deletions from the document could be made without affecting the overall usefulness? 6. is there any incorrect or misleading information (what and where)? 7. how would you improve this document?
? 2003 microchip technology inc. preliminary ds39616b-page 393 pic18f2331/2431/4331/4431 product identification system to order or obtain information, e. g., on pricing or delivery, refer to the factory or the listed sales office . sales and support part no. x /xx xxx pattern package temperature range device device pic18f2331/2431/4331/4431 (1) , pic18f2331/2431/4331/4431t (1,2) ; v dd range 4.2v to 5.5v pic18lf2331/2431/4331/4431 (1) , pic18lf2331/2431/4331/44310t (1,2) ; v dd range 2.0v to 5.5v temperature range i= -40 c to +85 c (industrial) package pt = tqfp (thin quad flatpack) so = soic sp = skinny plastic dip p=pdip ml = qfn pattern qtp, sqtp, code or special requirements (blank otherwise) examples: a) pic18lf4431-i/p 301 = industrial temp., pdip package, extended v dd limits, qtp pattern #301. b) pic18lf2331-i/so = industrial temp., soic package, extended v dd limits. c) pic18f4331-i/p = industrial temp., pdip package, normal v dd limits. note 1: f = standard voltage range lf = wide voltage range 2: t = in tape and reel - soic and tqfp packages only. data sheets products supported by a preliminary data sheet may have an errata sheet describing minor operational differences and recommended workarounds. to determine if an errata sheet exists for a particular device, please contact one of the following: 1. your local microchip sales office 2. the microchip corporate literature center u.s. fax: (480) 792-7277 3. the microchip worldwide site (www.microchip.com) please specify which device, revision of silicon and data sheet (include literature #) you are using. new customer notification system register on our web site (www.microchip.com/cn) to receive the most current information on our products.
ds39616b-page 394 preliminary ? 2003 microchip technology inc. americas corporate office 2355 west chandler blvd. chandler, az 85224-6199 tel: 480-792-7200 fax: 480-792-7277 technical support: 480-792-7627 web address: http://www.microchip.com atlanta 3780 mansell road, suite 130 alpharetta, ga 30022 tel: 770-640-0034 fax: 770-640-0307 boston 2 lan drive, suite 120 westford, ma 01886 tel: 978-692-3848 fax: 978-692-3821 chicago 333 pierce road, suite 180 itasca, il 60143 tel: 630-285-0071 fax: 630-285-0075 dallas 4570 westgrove drive, suite 160 addison, tx 75001 tel: 972-818-7423 fax: 972-818-2924 detroit tri-atria office building 32255 northwestern highway, suite 190 farmington hills, mi 48334 tel: 248-538-2250 fax: 248-538-2260 kokomo 2767 s. albright road kokomo, in 46902 tel: 765-864-8360 fax: 765-864-8387 los angeles 18201 von karman, suite 1090 irvine, ca 92612 tel: 949-263-1888 fax: 949-263-1338 phoenix 2355 west chandler blvd. chandler, az 85224-6199 tel: 480-792-7966 fax: 480-792-4338 san jose 1300 terra bella avenue mountain view, ca 94043 tel: 650-215-1444 toronto 6285 northam drive, suite 108 mississauga, ontario l4v 1x5, canada tel: 905-673-0699 fax: 905-673-6509 asia/pacific australia suite 22, 41 rawson street epping 2121, nsw australia tel: 61-2-9868-6733 fax: 61-2-9868-6755 china - beijing unit 706b wan tai bei hai bldg. no. 6 chaoyangmen bei str. beijing, 100027, china tel: 86-10-85282100 fax: 86-10-85282104 china - chengdu rm. 2401-2402, 24th floor, ming xing financial tower no. 88 tidu street chengdu 610016, china tel: 86-28-86766200 fax: 86-28-86766599 china - fuzhou unit 28f, world trade plaza no. 71 wusi road fuzhou 350001, china tel: 86-591-7503506 fax: 86-591-7503521 china - hong kong sar unit 901-6, tower 2, metroplaza 223 hing fong road kwai fong, n.t., hong kong tel: 852-2401-1200 fax: 852-2401-3431 china - shanghai room 701, bldg. b far east international plaza no. 317 xian xia road shanghai, 200051 tel: 86-21-6275-5700 fax: 86-21-6275-5060 china - shenzhen rm. 1812, 18/f, building a, united plaza no. 5022 binhe road, futian district shenzhen 518033, china tel: 86-755-82901380 fax: 86-755-8295-1393 china - shunde room 401, hongjian building no. 2 fengxiangnan road, ronggui town shunde city, guangdong 528303, china tel: 86-765-8395507 fax: 86-765-8395571 china - qingdao rm. b505a, fullhope plaza, no. 12 hong kong central rd. qingdao 266071, china tel: 86-532-5027355 fax: 86-532-5027205 india divyasree chambers 1 floor, wing a (a3/a4) no. 11, o?shaugnessey road bangalore, 560 025, india tel: 91-80-2290061 fax: 91-80-2290062 japan benex s-1 6f 3-18-20, shinyokohama kohoku-ku, yokohama-shi kanagawa, 222-0033, japan tel: 81-45-471- 6166 fax: 81-45-471-6122 korea 168-1, youngbo bldg. 3 floor samsung-dong, kangnam-ku seoul, korea 135-882 tel: 82-2-554-7200 fax: 82-2-558-5932 or 82-2-558-5934 singapore 200 middle road #07-02 prime centre singapore, 188980 tel: 65-6334-8870 fax: 65-6334-8850 taiwan kaohsiung branch 30f - 1 no. 8 min chuan 2nd road kaohsiung 806, taiwan tel: 886-7-536-4818 fax: 886-7-536-4803 taiwan taiwan branch 11f-3, no. 207 tung hua north road taipei, 105, taiwan tel: 886-2-2717-7175 fax: 886-2-2545-0139 europe austria durisolstrasse 2 a-4600 wels austria tel: 43-7242-2244-399 fax: 43-7242-2244-393 denmark regus business centre lautrup hoj 1-3 ballerup dk-2750 denmark tel: 45-4420-9895 fax: 45-4420-9910 france parc d?activite du moulin de massy 43 rue du saule trapu batiment a - ler etage 91300 massy, france tel: 33-1-69-53-63-20 fax: 33-1-69-30-90-79 germany steinheilstrasse 10 d-85737 ismaning, germany tel: 49-89-627-144-0 fax: 49-89-627-144-44 italy via quasimodo, 12 20025 legnano (mi) milan, italy tel: 39-0331-742611 fax: 39-0331-466781 netherlands p. a. de biesbosch 14 nl-5152 sc drunen, netherlands tel: 31-416-690399 fax: 31-416-690340 united kingdom 505 eskdale road winnersh triangle wokingham berkshire, england rg41 5tu tel: 44-118-921-5869 fax: 44-118-921-5820 11/24/03 w orldwide s ales and s ervice


▲Up To Search▲   

 
Price & Availability of PIC18F2431

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X